Flatten a nested JSON array
Flatten a nested array of JSON primitive values into one array in left-to-right order. Preserve null, booleans, strings and numbers as values. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess traverse nested arrays with explicit iterator frames.
Input
A JSON array whose elements are primitives or nested arrays.
Output
Print one compact JSON value: a flat JSON array.
Constraints
At most 10000 total array entries; nesting depth <= 100; primitive strings have length <= 100; numbers are integers with absolute value <= 1000000.
Example
Input
[1,[2,[3]],4]
Output
[1,2,3,4]
Example 2 input
[[[],[]]]
Output
[]
Boundary case: Empty nested arrays contribute no values
Public test cases (2)
Input
[1,[2,[3]],4]
Expected output
[1,2,3,4]
Input
[[],[true,null],["x"]]
Expected output
[true,null,"x"]
Hints (2)
- Array.isArray distinguishes arrays from primitive leaves
- Empty nested arrays contribute no values
Target complexity: time O(n) · space O(n+d), with depth d
