Merge Time-Sorted Tick Streams From Multiple Venues
Last updated: November 29, 2026
Quick Overview
Write a generator that takes several iterables of (timestamp, venue, price) ticks, each sorted by timestamp, and yields all ticks in global timestamp order without loading the streams into memory.
Voleon
November 29, 20260
0
0 solved
Write a generator that takes several iterables of (timestamp, venue, price) ticks, each sorted by timestamp, and yields all ticks in global timestamp order without loading the streams into memory.
Building a consolidated view of market data from many exchange feeds is everyday work for Voleon's data engineers. The interviewer checks heap-based k-way merge, lazy iteration with generators, deterministic tie-breaking, and what you do with a stream that violates its ordering promise.
What the Interviewer Expects
- Use a min-heap holding one head element per stream
- Keep streams lazy with iterators and a generator
- Break timestamp ties deterministically without comparing tick payloads
- Validate per-stream ordering and fail clearly
- State O(N log k) time and O(k) memory
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 handle a feed that can deliver ticks up to 50 ms late?
- What if one stream is much faster than the others: does the heap still help?
- How would you make this resumable after a crash midway through a day?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
k input iterables, each sorted by timestamp, possibly empty or infinite. Output: a generator in nondecreasing timestamp order. Ties should be stable (lower stream index first) so replays are reproduci...
High-Level Design
At any moment the next global tick is the smallest head among the k streams. A min-heap of (timestamp, stream_index, tick) gives that in O(log k). After popping from stream i, pull its next element ...