Round-Robin Iterator Over Multiple Response Streams
Last updated: November 29, 2026
Quick Overview
Implement an iterator that takes several iterables and yields one item from each in turn, skipping sources that are exhausted, until all are done.
OpenAI
November 29, 20260
0
0 solved
Implement an iterator that takes several iterables and yields one item from each in turn, skipping sources that are exhausted, until all are done.
Fair interleaving shows up when a server multiplexes several streaming completions or merges results from multiple tools, and it is a common OpenAI phone screen warm-up because it tests the iterator protocol in a few lines. The interviewer checks lazy consumption, correct removal of exhausted sources, and support for generators as well as lists.
What the Interviewer Expects
- Wrap each source with iter() so lists and generators both work
- Use a queue of active iterators for O(1) rotation
- Drop a source the first time it raises StopIteration
- Stay lazy: never materialize a source
- Test empty sources and uneven lengths
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
- Give each source a weight so it yields w items per turn.
- Add a peek() method without consuming the item.
- Make the sources asynchronous streams: what does the async version look like?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: any number of iterables, possibly empty or infinite.
- Output: items in round-robin order; exhausted sources are skipped.
- Must be lazy: items are pulled only when requested....
High-Level Design
Keep a deque of active iterators. To produce the next item, pop the front iterator and ask it for a value. On success, push it to the back and return the item; on StopIteration, simply do not push it ...