Skip to main content

Resumable Iterator

MediumPremium

You're building part of a batch pipeline. A job reads items, stops, and starts again later, and it must pick up exactly where it stopped without reprocessing or skipping anything. An ordinary iterator can't do that, because its position disappears with it.

Build an iterator whose whole position is one integer you can save and restore. Then make a list of these iterators behave as one, with a single global position across all of them.

Starter code and solution
Python 3.11+ (standard library)
Download code

What's in the codebase

  • iterator.py. ResumableIterator and CombinedIterator. The constructors and input validation are written, and the eight methods you implement raise NotImplementedError. This is the only file you change.
  • test_iterator.py. Eighteen tests: ten for ResumableIterator and eight for CombinedIterator, covering order, exhaustion, state round trips, resuming from a fresh iterator, invalid states, and empty inputs.
  • scenarios.py. Pauses and resumes a single iterator, then a combined one. Prints PASS when both resume correctly.
  • README.md. Run commands and ground rules.

Out of the box, the two construction tests pass because the constructors already reject non-list input. The other 16 error.

Your task

Implement next, hasNext, getState, and setState on both classes. Keep the camelCase method names, since the tests call them.

For ResumableIterator, the state is the index of the next item to return, from 0 to the list's length. next raises StopIteration when the iterator is exhausted. setState raises ValueError for anything that isn't an integer in range, and a bool counts as invalid even though Python treats True as 1.

For CombinedIterator, next walks the inner iterators in order, skipping exhausted and empty ones. getState returns the global index, the sum of the inner states. setState splits a global index back across the inner iterators, filling each one to its length before moving to the next, and validates against the total item count.

it = ResumableIterator([10, 20, 30, 40]); it.next(); it.next(); it.getState() -> 2 fresh = ResumableIterator([10, 20, 30, 40]); fresh.setState(2); fresh.next() -> 30 ResumableIterator(["a", "b"]).setState(3) -> ValueError ResumableIterator(["a", "b"]).setState(True) -> ValueError c = CombinedIterator([ResumableIterator([1, 2]), ResumableIterator([3]), ResumableIterator([4, 5])]) five calls to c.next() -> 1, 2, 3, 4, 5 c.setState(3); c.next() -> 4 CombinedIterator([]).hasNext() -> False

You're done when all 18 tests pass and python3 scenarios.py prints PASS.

The checkpoint follow-up

The interviewer probes the state design. You save state 2, then the job restarts against a list with a new item inserted at the front. What happens, and how would you detect it? What goes wrong if the caller advances an inner iterator directly while a CombinedIterator also uses it?

Using AI on this problem

Senior coding rounds like this often allow AI for lookups, such as why isinstance(True, int) is true. Decide the state model and the split rule for CombinedIterator.setState yourself, because those are what the interviewer is grading. If AI writes the validation, check that it rejects a bool before checking for int.