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
Coding & Algorithms
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
iterators
generators
queues
round robin
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported