Flatten Nested List Iterator
A medium-tier problem at 65% community acceptance, tagged with Stack, Tree, Depth-First Search. Reported in interviews at Mixpanel and 11 others.
Flatten Nested List Iterator shows up at Mixpanel, Netflix, OpenAI, LinkedIn, and Tesla. You're asked to design an iterator that flattens a nested list structure without materializing the entire flattened result upfront. The trick is deciding whether to use a stack or queue and when to unpack nested elements. With a 65% acceptance rate, most candidates who've seen the pattern solve it cleanly. But if you hit it cold and blank on the traversal order, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Flatten Nested List Iterator"
Flatten Nested List Iterator is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.
Get StealthCoderThe real challenge isn't the data structure, it's the design decision. A naive approach materializes the entire flattened list in memory during construction, which defeats the point of an iterator. The working solution uses a stack to defer unpacking nested lists until you actually call next(). When next() is called, you pop from the stack and, if it's a list, push its elements back on. If it's an integer, return it. The depth-first search pattern emerges naturally once you realize the stack lets you handle arbitrary nesting depths without recursion. Common failures: forgetting to handle empty nested lists, calling next() on an empty iterator, or materializing everything upfront. This is a design problem hiding inside a stack problem. If the pattern doesn't click during your assessment, StealthCoder runs live and skips the flailing phase.
Pattern tags
You know the problem.
Make sure you actually pass it.
Flatten Nested List Iterator recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Flatten Nested List Iterator interview FAQ
Is this really asked at FAANG-tier companies?+
Yes. Netflix, LinkedIn, Tesla, and Airbnb all report asking it. It's a medium-difficulty design problem that filters for candidates who understand iterators and lazy evaluation, not just basic data structures.
What's the key insight I'm missing if I'm failing test cases?+
You're probably materializing the entire flattened list upfront, which breaks the iterator contract. The trick is using a stack to defer unpacking nested lists until next() is called. Only process one element per call.
How does this relate to depth-first search?+
The iterator traverses the nested structure in DFS order. Each call to next() peels one level off by popping the stack and pushing children back on. It's DFS without recursion.
Stack or queue, which one?+
Stack. A queue would traverse breadth-first, which isn't the natural order for nested lists. Stack gives you DFS order and lets you process elements in the expected left-to-right, depth-first sequence.
How do I handle the hasNext() and next() contract correctly?+
hasNext() peeks without consuming, so ensure your stack-unwinding logic doesn't break. Keep unpacking nested lists in hasNext() until the top of the stack is either an integer or the stack is empty.
Want the actual problem statement? View "Flatten Nested List Iterator" on LeetCode →