Resumable List Iterator
Updated
Problem statement
Design a finite iterator over an integer array whose progress can be saved and restored.
Iterator Behavior
- Return the next value and signal exhaustion after the final value.
getState()returns a serializable, opaque state representing the current position.setState(state)restores the iterator so that subsequent values are exactly the values that remained when the state was saved.
A caller must not assume that a state is an array index. A saved state must continue to work after the original iterator has advanced or become exhausted.
State Restoration
Save the iterator state before reading the first value and after every returned value, including the exhausted position. Restore a fresh iterator from each saved state, read it to exhaustion, and return all resulting suffixes in save order.
Row i of the returned int[][] contains all values returned after restoring the state saved after exactly i values had been consumed. The final row is empty because it represents the exhausted state.
Testing
Write detailed tests before implementing a resumable iterator, beginning with a list and following with a file iterator. An abstract interface is supplied with iteration, get_state and set_state, and the tests must cover restoration from every position without assuming how state is represented.
Function
resumeSuffixes(values: int[]) → int[][]Examples
Example 1
values = [4, 7, 9]return = [[4, 7, 9], [7, 9], [9], []]The saved states represent the positions before 4, before 7, before 9, and after the iterator is exhausted. Restoring each state produces the corresponding suffix.
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Portfolio Tickers with Constant-Time Random PickCitadel · PHONE SCREENCitadel interview questions
- Design Browser HistoryBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Count Events in Rolling Minute WindowsMeta · PHONE SCREENMeta interview questions
- Build a Pivot Table with TotalsSigma Computing · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions