Round-Robin Iterator That Interleaves K Seller Offer Feeds
Last updated: November 29, 2026
Quick Overview
Given k iterators over seller offer feeds of different lengths, build an iterator that returns one offer from each feed in turn, skipping feeds that have run out.
Amazon
November 29, 20260
0
0 solved
Given k iterators over seller offer feeds of different lengths, build an iterator that returns one offer from each feed in turn, skipping feeds that have run out.
The Amazon offer listing page blends offers from many sellers fairly, which motivates round-robin interleaving. Interviewers check that you avoid scanning exhausted feeds repeatedly and that the iterator is lazy.
What the Interviewer Expects
- Use a queue of active iterators
- Pop a feed, take one item, re-enqueue if it still has items
- Never materialize all items up front
- O(1) amortized per next call
- Handle empty feeds at construction
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 sellers so a feed with weight 2 gets two items per round?
- How would you make this a Python generator?
- What if a feed is a remote stream that can block?
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: a list of k Python iterables. Output: an object with has_next() and next() that yields items in order feed 0 item 0, feed 1 item 0, ..., feed 0 item 1, and so on, skipping exhausted feeds. ...
High-Level Design
Wrap each iterable with a one-item lookahead so we can tell whether it is exhausted without losing an item. Place every non-empty wrapped feed in a deque. next() pops the left feed, takes its curren...