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.
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.
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.
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 🫡.