Merge Sorted Order Streams From Regional Shards Into One Iterator
Last updated: November 29, 2026
Quick Overview
Orders are stored in regional shards, and each shard exposes an iterator of (timestamp, order_id) in ascending time. Build an iterator that yields all orders across shards in global time order, pulling lazily so no shard is loaded fully into memory.
Walmart Labs
November 29, 20260
0
0 solved
Orders are stored in regional shards, and each shard exposes an iterator of (timestamp, order_id) in ascending time. Build an iterator that yields all orders across shards in global time order, pulling lazily so no shard is loaded fully into memory.
Walmart's order data is partitioned by region, and reconciliation and audit jobs need a single time-ordered view. Walmart Labs technical screens use this to check the k-way merge with a heap, correct iterator protocol, and lazy consumption.
What the Interviewer Expects
- Use a min-heap holding one head element per shard
- Pull the next item from a shard only after its head is consumed
- Break timestamp ties deterministically
- Implement the iterator protocol including StopIteration
- Analyze time as O(N log k) and memory as O(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 deduplicate an order that appears in two shards after a migration?
- What if one shard is slow to respond; how do you avoid blocking the merge?
- How would you checkpoint the merge so a crashed job resumes where it stopped?
- What if shards are only approximately sorted, within a few seconds?
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, each yielding (timestamp, order_id) tuples sorted by timestamp. Shards may be lists, generators or network cursors, and some may be empty. Output: an iterator over every tuple in...
High-Level Design
Classic k-way merge. Keep a heap with at most one entry per shard: the shard's current head. The smallest head is the next global item. After yielding it, pull the next element from that same shard an...