Merge Sorted Request Logs From Many Shards Into One Stream
Last updated: November 29, 2026
Quick Overview
Each API shard produces a log of request records already sorted by timestamp. Write a generator that lazily yields all records from all shards in global timestamp order, breaking ties by shard index, without loading every log into memory.
Stripe
November 29, 20260
0
0 solved
Each API shard produces a log of request records already sorted by timestamp. Write a generator that lazily yields all records from all shards in global timestamp order, breaking ties by shard index, without loading every log into memory.
Stripe serves API traffic from many shards and regions, and developer-facing request logs or internal debugging tools need a single ordered view. The interviewer checks that you reach for a k-way merge with a heap, keep it lazy so it works on large inputs and define a deterministic tie-break.
What the Interviewer Expects
- Use a min-heap holding one head record per shard
- Keep the merge lazy with iterators and a generator
- Break timestamp ties by shard index so output is reproducible
- Avoid comparing record payloads (dicts are not orderable)
- State complexity in terms of total records and number of shards
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 logs whose clocks are skewed by up to a few hundred milliseconds?
- How would you de-duplicate records that appear on two shards after a failover?
- How would you resume the merge after a crash without reprocessing everything?
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 iterables, each yielding (timestamp, record) in non-decreasing timestamp order. Output: a generator of (timestamp, shard_index, record) in global order. Inputs may be empty or of ...
High-Level Design
Seed a heap with (timestamp, shard_index, record) from the first item of each shard iterator. Pop the smallest, yield it, and pull the next item from the same shard. Including shard_index as the s...