Flatten Nested List Iterator
Flatten Nested List Iterator is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
Problem statement
You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
Implement the NestedIterator class:
- NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList.
- int next() Returns the next integer in the nested list.
- boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.
Your code will be tested with the following pseudocode:
initialize iterator with nestedList res = [] while iterator.hasNext() append iterator.next() to the end of res return res
If res matches the expected flattened list, then your code will be judged as correct.
Example 1:
Input: nestedList = [[1,1],2,[1,1]] Output: [1,1,2,1,1] Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
Example 2:
Input: nestedList = [1,[4,[6]]] Output: [1,4,6] Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
Constraints:
- 1 <= nestedList.length <= 500
- The values of the integers in the nested list is in the range [-106, 106].
Topics and companies
Topics: Stack, Tree, Depth-First Search, Design, Queue, Iterator.
Reported in interviews at Facebook, Amazon, Airbnb, Apple, LinkedIn, Bloomberg, Oracle.
How to practise Flatten Nested List Iterator on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N), where N is the total number of integers in the nested list. Each integer and list is visited exactly once. time and O(D), where D is the maximum depth of nesting. The stack holds at most D iterators at any time. space.
Open Flatten Nested List Iterator in the code editor, or read the Flatten Nested List Iterator editorial for a worked solution with its approach and complexity analysis.
Where Flatten Nested List Iterator sits in the DSA pattern sheet
- Design Patterns pattern guide
- Design (General/Specific) subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Flatten Nested List Iterator
Other problems that use the same Design (General/Specific) and Design Patterns techniques:
- All O`one Data Structure — Hard, same subpattern
- Design a Stack With Increment Operation — Medium, same subpattern
- Design a Text Editor — Hard, same subpattern
- Design Circular Deque — Medium, same subpattern
- Design Circular Queue — Medium, same subpattern
- Design Compressed String Iterator — Easy, same subpattern