HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Flatten an array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arrayflattenstackoverflow

Problem

I have got this interview question which has asked me to write a production level code which flattens the arbitrary nested array of arrays.

Code

/**
 * Flattens the array of arrays.
 *
 * @param {!Array} elements The nested array elements.
 * @return {!Array} The flat list of array objects.
 */
function flatten(elements) {
  return elements.reduce(function(result, current) {
    if (Array.isArray(current)) {
      return result.concat(flatten(current));
    }
    return result.concat(current);
  }, []);
};

/**
 * Compares two arrays.
 *
 * @param {!Array} a
 * @param {!Array} b
 * @template T
 * @return {boolean} Whether two arrays are equal.
 */
function assertArrayEqual(a, b) {
  // Guard clause
  if (a.length != b.length) {
    return false;
  }
  for (var i = a.length; i >= 0; i--) {
    if (a[i] !== b[i]) {
      return false
    }
  }
  return true;
};


Test Suite

I think it would be good to show some TDD skills but I have avoided to use some external library instead don't know if its a good idea from interviewers' point of view.

console.log(assertArrayEqual(flatten([]), flatten([])));
console.log(assertArrayEqual(flatten([1]), flatten([1])));
console.log(assertArrayEqual(flatten([1, 2]), flatten([1, 2])));
console.log(assertArrayEqual(flatten([1, 2, 3]), flatten([1, 2, 3])));
console.log(assertArrayEqual(flatten([1, 2, [3]]), flatten([1, 2, 3])));
console.log(assertArrayEqual(flatten([1, 2, [3], [4, [5]]]), flatten([1, 2, 3, 4, 5])));
console.log(assertArrayEqual(flatten([[1, 2, [3]], 4]), flatten([1, 2, 3, 4])));


Questions

-
I haven't really handled an error condition here, should I check if the passed input is an array type or not?

-
Seeing the simplicity of the question I have avoided the added complexity of the OOP and followed a functional approach instead.

-
I think I have covered the documentation well but any suggestions are welcomes(I think it can be modified if I decide to throw an exception).

-
It seemed overkill to us

Solution

-
Depends. If this is internal code, which is covered by your compiler, then there's no sense guarding. Your compiler already know who calls it and what's being passed in. However, if it's the public-facing code, I would guard against unexpected input.

-
Totally acceptable. Besides, what's there to OOP when you're just manipulating array structure. Also, for fun, technically you're doing OOP. Arrays are objects in JS. In fact, everything aside from primitives stem from Object.

-
No comment.

-
If you plan to make more of these functions, consider namespacing them. Making them a property of one global object would suffice.

-
If you are adventurous, you can also try out TypeScript. The only difference is that type hints are now in the language rather than just annotations.

-
See following.

function flatten(elements) {
  return elements.reduce((result, current) => {
    return result.concat(Array.isArray(current) ? flatten(current) : current);
  }, []);
};

Code Snippets

function flatten(elements) {
  return elements.reduce((result, current) => {
    return result.concat(Array.isArray(current) ? flatten(current) : current);
  }, []);
};

Context

StackExchange Code Review Q#133105, answer score: 7

Revisions (0)

No revisions yet.