Flatten Nested Array
Examples
Example 1:
Input: nested = [[1, 2, 3], [4, 5], [6]]
Output: [1, 2, 3, 4, 5, 6]
Explanation:
Example 2:
Input: nested = [[1], [], [2, 3]]
Output: [1, 2, 3]
Explanation:
Example 3:
Input: nested = []
Output: []
Example 1:
Input: nested = [[1, 2, 3], [4, 5], [6]]
Output: [1, 2, 3, 4, 5, 6]
Explanation:
Example 2:
Input: nested = [[1], [], [2, 3]]
Output: [1, 2, 3]
Explanation:
Example 3:
Input: nested = []
Output: []
Given a nested array of integers, return a single array containing every integer in left-to-right order.
A nested array is an array whose elements are either an integer or another nested array. The nesting can be of arbitrary depth.
The interviewer expects two solutions: one written recursively, and one written iteratively (no recursion). The reference solution shows both.
flatten([1, [2, [3, 4], 5], 6]) -> [1, 2, 3, 4, 5, 6]
flatten([[1, 2, 3], [4, 5], [6]]) -> [1, 2, 3, 4, 5, 6]
flatten([]) -> []
This question was reported from a Roblox Frontend Engineer phone screen (June 2025). The candidate had to write both a recursive and a non-recursive (stack-based) version on the same call.
Each sub-array is concatenated in order.
Empty sub-arrays contribute nothing.
0 <= nested.length <= 10^4number[][]); your recursive solution should still generalize to arbitrary depth — that is what the interviewer evaluates.