Nested Array Generator - Problem

Given a multi-dimensional array of integers, return a generator object which yields integers in the same order as inorder traversal.

A multi-dimensional array is a recursive data structure that contains both integers and other multi-dimensional arrays.

Inorder traversal iterates over each array from left to right, yielding any integers it encounters or applying inorder traversal to any arrays it encounters.

Input & Output

Example 1 — Simple Nested Array
$ Input: arr = [1,[2,3],4]
Output: [1,2,3,4]
💡 Note: First yield 1, then traverse [2,3] yielding 2 then 3, finally yield 4
Example 2 — Deeply Nested
$ Input: arr = [1,[2,[3,4]],5]
Output: [1,2,3,4,5]
💡 Note: Traverse left-to-right: yield 1, enter [2,[3,4]], yield 2, enter [3,4], yield 3,4, then yield 5
Example 3 — Empty Nested Arrays
$ Input: arr = [1,[],2,[3,[]]]
Output: [1,2,3]
💡 Note: Empty arrays [] are skipped, yielding only the integers 1, 2, and 3

Constraints

  • 1 ≤ arr.length ≤ 500
  • 0 ≤ arr[i] ≤ 1000 or arr[i] is a valid multi-dimensional array
  • The depth of nesting will not exceed 100

Visualization

Tap to expand
Nested Array GeneratorINPUT1[2,3]4Multi-dimensional ArrayINORDER TRAVERSALOUTPUT1234Generator Sequence
Understanding the Visualization
1
Input
Multi-dimensional array [1,[2,3],4]
2
Traverse
Inorder traversal yields integers left-to-right
3
Output
Generator yields sequence [1,2,3,4]
Key Takeaway
🎯 Key Insight: Generators provide lazy evaluation - elements are only processed when requested, making it memory efficient for large nested structures
Asked in
Google 25 Facebook 18 Amazon 15
23.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen