Implement a deepFlattenArray function
Deep Flattening refers to the process of transforming a nested array into a single-level array by flattening all nested arrays within it. We already have the Array.prototype.flat()
method available to use, but in this case we are recreating it.
1. Basic implementation
Flatten the array to for any level of nesting.
function deepFlattenArray(arr) { const result = [];
function deepFlattenArrayImpl(input) { input.forEach((item) => { if (Array.isArray(item)) { deepFlattenArrayImpl(item); } else { result.push(item); } }); }
deepFlattenArrayImpl(arr);
return result;}
// Usageconst inputArr = [1, [2, [3, [4, [{ name: 'John Doe' }, null], 0], -1]]];const output = deepFlattenArray(inputArr);
/** * Yields: * [ 1, 2, 3, 4, { "name": "John Doe" }, null, 0, -1] */
console.log(output);
2. With Depth implementation
In the previous case, we simply flattened the array to for any level of nesting. This variation will have a depth field upto which the flattening needs to be performed.
As most of the implementation remains exactly same, I will highlight only the newer changes made.
function deepFlattenArray(arr, depth = 1) { const result = [];
function deepFlattenArrayImpl(input, depth) { input.forEach((item) => { // if flattening is needed and depth is not exhausted if (Array.isArray(item) && depth > 0) { deepFlattenArrayImpl(item, depth - 1); } else { result.push(item); } }); }
deepFlattenArrayImpl(arr, depth);
return result;}
// Usageconst inputArr = [1, [2, [3, [4, [{ name: 'John Doe' }, null], 0], -1]]];const output = deepFlattenArray(inputArr, 1);
/** * Yields: [ 1, 2, [ 3, [ 4, [ { "name": "John Doe" }, null ], 0 ], -1 ]] */
console.log(output);
3. As polyfill implementation
As Array.prototype.flat()
is already present to be used on arrays, in an interview setting one might also expect to implement a polyfill for this. The solution to that is also staright forward and only requires an understanding of this
keyword and prototype
property on constructor functions.
if (!Array.prototype.flat) { Array.prototype.flat = function (depth = 1) { const arr = this; // original array should not be mutated return deepFlattenArray(arr, this); };}
Further Reading
I strongly encourage you to explore and tackle additional questions in my Recursion Questions for Frontend Interviews blog series.
By doing so, you can enhance your understanding and proficiency with recursion, preparing you to excel in your upcoming frontend interviews.
Wishing you best. Happy Interviewing 🫡.