Flatten Nested List Iterator
Updated
Problem statement
Build an iterator over a nested list of integers. Each item in the list is either a signed integer or another list. The iterator hands out the integers one at a time, in the order you meet them when you read the list from left to right and step into each inner list as you reach it. An empty list adds no values.
The string nestedList holds that list written as valid JSON. Create one iterator, then run the calls in operations in order:
HAS_NEXTtells you whether an integer is still left. It does not use up the integer, so calling it again sees the same next value.NEXTreturns the next integer and moves past exactly that one. It can be called without aHAS_NEXTcheck first. An integer is always left wheneverNEXTis called.
Return one string for each operation: "true" or "false" for HAS_NEXT, and the integer itself for NEXT, written as a plain decimal number with no plus sign or extra leading zeros. Repeated integers, negative values and zero are all returned like any other value.
The calls may stop before every integer has been read. Return results only for the calls that were made. The nested list never changes, and there is no remove call.
Function
runNestedIterator(nestedList: String, operations: String[]) → String[]Examples
Example 1
nestedList = "[[1,1],2,[1,1]]"operations = ["HAS_NEXT","HAS_NEXT","NEXT","NEXT","HAS_NEXT","NEXT","NEXT","NEXT","HAS_NEXT"]return = ["true","true","1","1","true","2","1","1","false"]Both HAS_NEXT calls at the start see the same first integer, so neither one uses it up. The five NEXT calls then return 1, 1, 2, 1, 1, in list order. By the last HAS_NEXT call every integer has been read, so it returns "false".
Example 2
nestedList = "[1,[4,[6]]]"operations = ["NEXT","HAS_NEXT","NEXT","HAS_NEXT","NEXT","HAS_NEXT"]return = ["1","true","4","true","6","false"]The calls read the integers from left to right, stepping into each inner list as they reach it: 1, then 4, then 6. Nothing is left after that, so the last HAS_NEXT returns "false".
Example 3
nestedList = "[[],[7,[]],[]]"operations = ["HAS_NEXT","NEXT","HAS_NEXT","HAS_NEXT"]return = ["true","7","false","false"]The empty lists hold no integers, so the only value is 7. After NEXT returns it, both later HAS_NEXT calls return "false".
Constraints
nestedListis valid JSON that holds only arrays and integers. The outer value is an array with1to500items.- Every integer is in the range
-10^6 <= value <= 10^6. Empty inner arrays and JSON whitespace are allowed. - There are at most
2000integers and arrays in total, counting the outer array. Arrays are nested at most30levels deep, andnestedListhas at most20000characters. 1 <= operations.length <= 1000. Each operation isHAS_NEXTorNEXT, and an integer is always left wheneverNEXTis called.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- KV Store with Nested TransactionsApplied Intuition · PHONE SCREEN · ONSITE INTERVIEWApplied Intuition interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Multiline Text Editor with Cursor MovementDatologyAI · PHONE SCREEN
- Layer History with Batch Undo and RedoFigma · OA · PHONE SCREENFigma interview questions
- Stack with Constant-Time Middle QueriesAmazon · PHONE SCREEN
- In-Memory Database with Transactions and SavepointsNextdoor · PHONE SCREENNextdoor interview questions
- Throne Inheritance Without an Initial KingSnowflake · PHONE SCREENSnowflake interview questions
- Find File Paths in a Directory TreeUber · PHONE SCREENUber interview questions