TypeScriptMediumRecursion25 min

Flatten a nested numeric array

Flatten a nested array of numbers into a numeric array in left-to-right depth-first order. Empty nested arrays contribute no elements. Inputs are valid JSON and must not be modified unless the algorithm uses a local copy. This exercise probes typed data modeling, boundary handling, and algorithm selection.

Input

One JSON value: Nested[]. See the sample for its exact shape.

Output

One compact JSON value of type number[]. Strings include JSON quotes; arrays preserve the specified order.

Constraints

Array/string lengths are 0 to 10000 unless stated otherwise. Numbers are integers in [-1000000,1000000]; all computed results fit the safe integer range. Nested inputs have depth at most 100.

Example

Input

[1,[2,[3]],4]

Output

[1,2,3,4]

Example 2 input

[[1,2],[],[3,[4]]]

Output

[1,2,3,4]

Checks a boundary or contrasting case beyond the public examples.

Public test cases (2)

  • Input

    [1,[2,[3]],4]

    Expected output

    [1,2,3,4]
  • Input

    []

    Expected output

    []
Hints (2)
  1. Start with the required output order and the empty-input behavior.
  2. Recurse only for array nodes and append numeric leaves

Target complexity: time O(n) · space O(n+d)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All TypeScript programs