Lazy Iterator Merging Sorted Order Streams From Many Regions
Last updated: November 29, 2026
Quick Overview
Given several per-region iterators that each yield (timestamp, order_id) in timestamp order, build an iterator that yields all orders in global timestamp order without loading any stream fully into memory.
DoorDash
November 29, 20260
0
0 solved
Given several per-region iterators that each yield (timestamp, order_id) in timestamp order, build an iterator that yields all orders in global timestamp order without loading any stream fully into memory.
DoorDash data flows through many regional partitions, and backfills or audit jobs often need a single ordered view. The interviewer checks k-way merge with a heap, laziness (holding one pending item per stream), and the Python iterator protocol.
What the Interviewer Expects
- Hold exactly one pending element per stream in a heap
- Break timestamp ties deterministically by stream index
- Implement __iter__ and __next__ correctly
- Support generators, not just lists
- State O(log k) per element
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 merge streams that are only approximately sorted (up to 5 seconds late)?
- How would you checkpoint the merge so a crashed job can resume?
- What if one stream is far slower to produce than the others?
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: k iterables of
(ts, order_id), each sorted byts. - Output: an iterator over all items sorted by
ts; ties go to the lower stream index. - Memory must be O(k), not O(total items)....
High-Level Design
Convert each input to an iterator and prime a heap with its first element as (ts, stream_index, item). Including the index means ties never compare the payloads and stay deterministic. __next__ po...