Build a Resumable Iterator Over Sharded Training Files
Last updated: November 29, 2026
Quick Overview
Implement an iterator that yields records across an ordered list of data files and supports `get_state()` and `set_state(state)` so a training job can checkpoint its position and resume exactly where it stopped, even when some files are empty.
OpenAI
November 29, 20260
0
0 solved
Implement an iterator that yields records across an ordered list of data files and supports `get_state()` and `set_state(state)` so a training job can checkpoint its position and resume exactly where it stopped, even when some files are empty.
Training runs at OpenAI stream data from many shards and must resume from checkpoints after preemption without repeating or skipping examples. This question checks iterator protocol fluency, compact and serializable state, and correct handling of empty shards and exhaustion, with follow-ups usually moving toward multiple workers or shuffled order.
What the Interviewer Expects
- Implement `__iter__` and `__next__` correctly, raising StopIteration at the end
- Keep state minimal and JSON-serializable (file index plus offset)
- Skip empty files without special cases leaking into the state
- Guarantee that restoring a state yields exactly the remaining sequence
- Validate restored state
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Files are too large to hold in memory: what state do you store for a byte-offset reader?
- Make the order shuffled per epoch but still resumable.
- Several data-loader workers each read a disjoint subset: how is global state saved?
- How would you make the iterator thread-safe?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Constructor receives the ordered list of files (here each file is a list of records).
- Iteration yields records in file order, then record order.
get_state()returns a small serializable value;...
High-Level Design
The full position is captured by two integers: the index of the current file and the offset of the next record within it. __next__ first advances past exhausted or empty files, then returns the reco...