Skip to content

Instantly share code, notes, and snippets.

@AlexJuarez
Last active October 21, 2016 23:08
Show Gist options
  • Save AlexJuarez/962ff30d82444b768345c3b2101d0541 to your computer and use it in GitHub Desktop.
Save AlexJuarez/962ff30d82444b768345c3b2101d0541 to your computer and use it in GitHub Desktop.

Front End Engineering question Given an array, return it's flattend structure.

[1, [2, [3]]] -> [1, 2, 3]

[1, [2, 3], 4] -> [1, 2, 3, 4]

possible total 24 points

  • result [4]
    • correctly flattens [4/4]
    • partially flattens [2/4]
  • syntax [5]
    • uses === throughout except for null checking [1/1]
    • solution is pure (produces no side effects such as modifying the input) [4/4]
  • solution recusive/ iterative [13]
    • solution is recusive [6/13]
      • loop type [2]
        • uses for or while loop incrementing not in reverse [2/2]
        • uses forEach, map etc. [1/2]
    • solution is iterative [7/7]
      • approach [4]
        • uses concat and reverse [4/4] see example 1.
        • uses unshift [2/4]
      • loop type [2]
        • uses for(;;) { break; } [1/2]
        • uses while [2/2]
  • array checking [2]
    • full points [2/2]
      • javascript toString.call(o) === '[object Array]'
      • javascript o instanceof Array [2/2]
      • javascript Array.isArray(o)
    • any other array checking method that works but not in all cases [1/2]
      • typeof o === 'object'

example 1. iterative [24/24]

module.exports = function flatten(arr) {
  let output = [];

  let queue = [].concat(arr);
  while (queue.length) {
    let o = queue.pop();
    if (o instanceof Array) {
      queue = queue.concat(o);
    } else {
      output.push(o);
    }
  }

  return output.reverse();
};

example 2. iterative alternative [22/24]

module.exports = function flatten(arr) {
  let output = [];

  let queue = [].concat(arr);
  while (queue.length) {
    let o = queue.pop();
    if (o instanceof Array) {
      queue = [].unshift.apply(queue, o);
    } else {
      output.push(o);
    }
  }

  return output;
};

example 3. recursive [19/24]

function flatten(arr) {
  var output = [];

  return flattenHelper(arr, output);
};

function flattenHelper(arr, output) {
  var o;
  for (var i = 0; i < arr.length; i++) {
    o = arr[i];
    //Check to see if the item is an array
    if (typeof o === "object" && typeof o.length === "number") { //goofy check but works in all cases [2/2]
      flattenHelper(o, output);
    } else {
      output.push(o);
    }
  }

  return output;
};

example 4. recusive alternative [19/24]

function flattenArr(arr) {
  this.flatArr = this.flatArr || [];
  var i;

  for (i=0; i<arr.length; i++) {
    if (arr[i] instanceof Array) {
            flattenArr.call(this,arr[i]);
    } else {
        this.flatArr.push(arr[i]);
    }
  }

  return flatArr;
}

example 5. partial iterative flatten [14/24]

function flatten(arr){
  var new_array = [];
  for (var idx = 0; idx < arr.length; idx++) {
    if(typeof arr[idx] == "object"){
      for(var item = 0; item < arr[idx].length; item++){
        new_array.push(arr[idx][item]);
      }
    }
    else{
      new_array.push(arr[idx]);
    }
  }
  return new_array;
}

example 6. failing flatten, but would not have side effects [4/24] works in the case [[1], 2, 3] => [1, 2, 3]

function flatten(arr) {
  return arr.reduce(function (a, b) {
    return a.concat(b);
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment