Quant Memo
Coding/●●●●

Reconstruct the prevailing spread at each trade

Asked at Millennium, IMC

You have three time-sorted streams: trades (ts, price), best-bid updates (ts, bid), and best-ask updates (ts, ask). For every trade, recover the prevailing best bid and best ask (each the latest update at or before the trade) and report the spread ask - bid at execution time. This is how effective-spread and price-improvement stats are built when the bid and ask arrive as two independent feeds.

bids   = [(1, 99.9), (4, 100.1)]
asks   = [(2, 100.2), (5, 100.4)]
trades = [(3, 100.0), (6, 100.3)]
->
[(3, 100.0, 99.9, 100.2, 0.3),      # last bid ts=1, last ask ts=2
 (6, 100.3, 100.1, 100.4, 0.3)]     # last bid ts=4, last ask ts=5

Implement the three-way join in O(n+b+a)O(n + b + a), one pass over each stream.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions