LeetCode Problem Workspace

Nested Array Generator

Implement a generator that performs inorder traversal on a multi-dimensional array of integers.

category

0

Topics

code_blocks

1

Code langs

hub

0

Related

Practice Focus

Medium · Nested Array Generator core interview pattern

bolt

Answer-first summary

Implement a generator that performs inorder traversal on a multi-dimensional array of integers.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Nested Array Generator core interview pattern

Try AiBox Copilotarrow_forward

To solve the Nested Array Generator problem, you need to implement a generator function that performs an inorder traversal of a nested array structure. The function should yield integers in the same order as they appear from left to right, including traversing any nested subarrays. This problem involves both recursion and generator functions to handle multi-dimensional arrays efficiently.

Problem Statement

Given a multi-dimensional array of integers, implement a generator that performs an inorder traversal of the array and yields the integers in the order they appear.

In this problem, arrays can contain other arrays. You must traverse these nested arrays and yield integers in the same order, processing each level recursively. The goal is to create a generator function that uses the 'yield' syntax to return integers, one at a time, in the correct sequence as they are encountered during the traversal.

Examples

Example 1

Input: arr = [[[6]],[1,3],[]]

Output: [6,1,3]

const generator = inorderTraversal(arr); generator.next().value; // 6 generator.next().value; // 1 generator.next().value; // 3 generator.next().done; // true

Example 2

Input: arr = []

Output: []

There are no integers so the generator doesn't yield anything.

Constraints

  • 0 <= arr.flat().length <= 105
  • 0 <= arr.flat()[i] <= 105
  • maxNestingDepth <= 105

Solution Approach

Use Recursion to Traverse Nested Arrays

Start by defining a recursive generator that traverses the array. For each element in the array, check if it is a sub-array. If it is, call the generator recursively. If it is an integer, yield it directly.

Leverage the 'yield*' Syntax

In order to yield elements from nested sub-arrays, use the 'yield*' syntax. This syntax delegates control to another generator, allowing you to handle nested arrays and yield their elements in order.

Handle Edge Cases and Empty Arrays

Ensure your solution handles edge cases such as empty arrays. The generator should return an empty sequence when no integers are present in the array, which is a common failure mode.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity depends on the total number of elements in the input array, while the space complexity depends on the recursion depth and the number of elements in the array. In the worst case, the recursion depth could be proportional to the maximum nesting depth of the array, and the space complexity could scale with the total number of elements in the array.

What Interviewers Usually Probe

  • Understand recursion with generator functions.
  • Familiarity with 'yield*' syntax for delegating generator control.
  • Ability to optimize traversal of nested arrays.

Common Pitfalls or Variants

Common pitfalls

  • Failing to correctly implement the recursion for nested arrays.
  • Not using the 'yield*' syntax to properly delegate control between nested generators.
  • Forgetting to handle edge cases like empty arrays.

Follow-up variants

  • Implement a version that flattens the array into a single list before traversing.
  • Optimize for large arrays with a higher nesting depth.
  • Test how the solution performs with arrays containing only integers versus arrays with mixed data types.

FAQ

What is the core interview pattern for the Nested Array Generator problem?

The core pattern involves implementing a recursive generator function that performs an inorder traversal of a multi-dimensional array, yielding integers in sequence.

How do you handle nested arrays in this problem?

Use recursion and the 'yield*' syntax to delegate control to sub-arrays and yield their elements in the correct order.

How does the 'yield*' syntax work in the context of nested arrays?

The 'yield*' syntax delegates control to another generator, allowing you to traverse and yield elements from nested arrays efficiently.

What are some common mistakes when solving the Nested Array Generator problem?

Common mistakes include not using recursion properly, failing to handle empty arrays, and not using the 'yield*' syntax to delegate to nested generators.

How does GhostInterview help with solving this problem?

GhostInterview provides step-by-step guidance on recursive problem solving, generator function usage, and optimization for large arrays, making it easier to handle complex interview questions like Nested Array Generator.

terminal

Solution

Solution 1

#### TypeScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type MultidimensionalArray = (MultidimensionalArray | number)[];

function* inorderTraversal(arr: MultidimensionalArray): Generator<number, void, unknown> {
    for (const e of arr) {
        if (Array.isArray(e)) {
            yield* inorderTraversal(e);
        } else {
            yield e;
        }
    }
}

/**
 * const gen = inorderTraversal([1, [2, 3]]);
 * gen.next().value; // 1
 * gen.next().value; // 2
 * gen.next().value; // 3
 */
Nested Array Generator Solution: Nested Array Generator core interview… | LeetCode #2649 Medium