Round-Robin Iterator Across Several Story Trays
Last updated: November 29, 2026
Quick Overview
Given k lists of story ids, implement an iterator that yields one item from each list in turn (list 0, list 1, and so on), skipping lists once they run out, until every item has been returned.
Meta
November 29, 20260
0
0 solved
Given k lists of story ids, implement an iterator that yields one item from each list in turn (list 0, list 1, and so on), skipping lists once they run out, until every item has been returned.
Instagram can interleave stories from several sources so no single source dominates the tray. Meta interviewers use this to check queue-based round-robin over iterators and clean handling of uneven and empty lists.
What the Interviewer Expects
- Use a queue of iterators rather than index arithmetic
- Drop exhausted iterators so each step stays O(1)
- Handle empty input lists
- Keep has_next side-effect free from the caller's view
- Generalize from two lists to k
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
- How would you weight sources, taking two from one tray per turn?
- What if the underlying lists are infinite generators?
- How would you resume iteration after a crash?
- How would you interleave by timestamp instead of round-robin?
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 is k lists, possibly of different lengths and possibly empty.
- Output order: first element of each non-empty list in list order, then the second of each, and so on.
- Expose
has_next()and ...
High-Level Design
Put an iterator for each list into a deque. To produce the next value, pop the front iterator and pull one item. If it yields, push the iterator to the back. If it is exhausted, drop it and try the ne...