Resumable Iterator With Save and Restore Over Nested Lists and Concurrent Use
Company: OpenAI
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Technical Screen
Implement a resumable iterator: an iterator whose position can be saved as a state at any moment and restored later, so that iteration continues exactly where it stopped, including in a new iterator instance over the same data. The problem grows in parts: first a flat list, then nested lists up to three dimensions, and finally a version that stays correct under concurrent use.
The source names the problem and this progression but not the exact interface. Assume the following unless the interviewer specifies otherwise:
- `next()` returns the next element, or raises `StopIteration` once every element has been returned.
- `get_state()` returns a plain, serializable value (for example a dict that converts to JSON) that describes the current position.
- `set_state(state)` moves the iterator to the position a state describes. It accepts any state that `get_state()` produced on an iterator over the same data, including one from a different instance.
### Constraints and Clarifications
- The data does not change while iterators over it are in use.
- Saving a state and then advancing the iterator must leave the saved state unchanged.
- Elements come out in index order for a flat list and in row-major order for nested lists.
### Clarifying Questions
- Must a state survive a process restart (written to disk and loaded later), or only work within one process?
- What should `set_state` do with a state that does not fit the data, such as an index past the end?
- Can nested lists contain empty inner lists, and can inner lists have different lengths?
- In the concurrent part, does "concurrent" mean several threads sharing one iterator, or an asynchronous iterator whose elements arrive through I/O?
### Part 1: Resumable iterator over a flat list
Implement `ResumableListIterator(items)` with `next()`, `get_state()` and `set_state(state)`. Take a state after any number of `next()` calls and restore it into a fresh iterator over the same list: the fresh iterator must return exactly the elements the original would have returned from that point on.
```hint Decide what a position is
Find the smallest piece of information that fully describes how far the iteration has gone.
```
#### What This Part Should Cover
- What the state contains and why that is enough to resume.
- Behavior at the start, in the middle and after the last element.
- Saved states that later calls cannot modify.
### Part 2: Two- and three-dimensional lists
Extend the iterator to a two-dimensional list (a list of lists), then to a three-dimensional list. It returns the innermost elements in row-major order and skips empty inner lists. `get_state()` and `set_state()` must work at every position, including positions next to empty inner lists and the position after the last element.
```hint Count the levels
Compare what a position must record with one level of lists against what it must record with two or three levels.
```
#### What This Part Should Cover
- A state that identifies the position at every level.
- Empty inner lists at any level, including at the start and the end.
- How the design extends from two levels to three and beyond.
### Part 3: Concurrent use
Make the iterator correct under concurrent use. Assume several threads share one iterator instance and call `next()` and `get_state()` at the same time; if the interviewer means an asynchronous iterator, answer the same questions for coroutines. Every element must be returned exactly once across all threads, and a state taken during concurrent use must describe a well-defined point in the iteration.
```hint Find the step that cannot be split
Look for the reads and writes inside one call that another thread must never see half done.
```
#### What This Part Should Cover
- Which operations must be atomic and how they are protected.
- What a state taken while other threads are calling `next()` means.
- Elements that were handed out but not yet processed when a state is saved or a consumer fails.
### What a Strong Answer Covers
- One interface shared by the flat, nested and concurrent versions.
- States that are small, serializable and valid across instances.
- Rejection of states that do not fit the data.
- Tests that save and restore at every position, including the end.
### Follow-up Questions
- How would you make the iterator resumable when the data is a sequence of large files read in order, so that a state has to point inside a file?
- How would you support any nesting depth, including lists that mix elements and sublists at the same level?
- If data could be appended while iteration is in progress, what would a state need to record?
- How would you test the concurrent version so that a race shows up reliably?
Overview: A coding interview problem that asks for an iterator whose position can be saved and restored, first over a flat list, then over two- and three-dimensional nested lists, and finally under concurrent use. It tests state design, empty inner lists, state validation and thread safety.
Read the full OpenAI Software Engineer interview experience this question came from