Merge Timestamped Trade Feeds From Several Exchanges Deterministically

Last updated: November 29, 2026

Quick Overview

Given per-exchange lists of trades already sorted by (timestamp, sequence number), produce one consolidated stream ordered by timestamp, breaking timestamp ties by a fixed exchange priority and then by sequence number.

D. E. Shaw
Coding & Algorithms
Software Engineer
D. E. Shaw
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given per-exchange lists of trades already sorted by (timestamp, sequence number), produce one consolidated stream ordered by timestamp, breaking timestamp ties by a fixed exchange priority and then by sequence number.

A warm-up in the style of a D. E. Shaw online assessment for software developers, framed around consolidating market data for research and backtesting. The interviewer checks that you reach for a k-way merge instead of concatenate-and-sort and that the output is fully deterministic so two backtest runs see identical event order.

What the Interviewer Expects
  • Uses a min-heap keyed on (timestamp, exchange priority, sequence) holding one cursor per feed
  • States O(N log k) time and O(k) extra space for N trades across k feeds
  • Explains why a deterministic tie-break matters for reproducible backtests
  • Handles empty feeds and a single feed without special cases
  • Avoids comparing full trade tuples that may contain incomparable fields
Key Topics to Cover
k-way merge
heaps
stable ordering
market data
reproducibility
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you make this streaming when feeds arrive over sockets and one exchange lags?
  • What changes if some feeds are not internally sorted because of out-of-order packets?
  • How would you merge feeds that are each too large to fit in memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: a dict from exchange name to a list of trades (ts, seq, symbol, price, qty), each list sorted by (ts, seq), plus a priority map. Output: one list ordered by timestamp, then exchange priorit...

High-Level Design

Keep one cursor per feed and a min-heap of the head trade of each feed. Pop the smallest, emit it, and push that feed's next trade. The heap key is (ts, priority, seq, exchange, index). Putting the ...


Submit Your Answer
Markdown supported

Related Questions