Problem, as an interviewer would state it: "Implement a function to flatten a nested array completely, iteratively (no recursion) using a stack. It must correctly handle sparse-array holes and circular references."
Examples:
flatten([1, [2, [3, [4, [5]]]], 6]); // [1, 2, 3, 4, 5, 6]
flatten([1, , [2, , [, 3]], , 4]); // [1, 2, 3, 4] (holes are skipped)
Clarifying questions expected:
- What should a circular reference do — throw, skip, or stop early?
- Should an array that is merely shared (referenced twice, but not inside itself) be flattened each time?
- Should there be a depth limit, matching
Array.prototype.flat(depth)?
Code / implementation expected: Yes — real, direct proof against native flat, at depths where recursion (and native flat) fail.