Skip to content

Instantly share code, notes, and snippets.

Last active February 22, 2020 18:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
array flat arbitrary
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
Your solution should be a link to a gist on with your implementation.
When writing this code, you can use any language you're comfortable with. The code must be well tested and documented. Please include unit tests and any documentation you feel is necessary. In general, treat the quality of the code as if it was ready to ship to production.
Try to avoid using language defined methods like Ruby's Array#flatten or JavaScript's Array.flat.
var list = [1,2, [3, 4, [5, 6]]];
/* @function flat
@summary flatten arbitrarily nested arrays without using the native feature
@description this isn't reality, it's an abitrary communication exercise
// flatten an array
var list = [1,2, [3, 4, [5, 6]]];
// returns a new array [1,2,3,4,5,6] like `array.flat(Infinity)` method
function flat(list, val, i, arr){
val.reduce(flat, list);
return list;
return list.reduce(flat, []);
var result = flat(list);
console.log(`assertions: ${ [
console.assert(result.join() === list.flat(Infinity).join(), 'native and manual results match')
,console.assert(result.join('') === '123456', 'result is explicitly checked')
].length }
against method "flat"
input: ${ list.join(',') }
output: ${ result.join(',') }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment