Market Data Gap Recovery
Prerequisites: Market Data Feed Handler Shape, Packet Drops and Backpressure, TCP vs UDP Trade-Offs
The feed handler in Market Data Feed Handler Shape detects a gap by comparing each update's sequence number with the one it expects. This problem builds what the handler does with the answer.
A trading screen shows the best prices for AAPL, built from a UDP feed in which most messages are small changes to the previous state. Each change is correct only if every earlier change was applied.
If sequence 42 arrives right after 40, applying 42 produces prices that look plausible and are wrong.
- Book: the record of what buyers and sellers currently want in one instrument. Buyers' interest forms the bid side and sellers' interest the ask side.
- Price level: one price together with its size, the number of shares wanted at that price. Each side is a set of price levels.
- Best bid and best ask: the highest bid price and the lowest ask price. Together they are the top of book.
- Snapshot: a message that lists every price level on both sides and replaces whatever the receiver held.
- Delta: a message that changes one price level. It is meaningful only relative to the book left by the message before it.
What to implement
process_events(lines)returns the lines to print, in order.linesholds the feed lines in arrival order.- On stdin, the first line is the number of lines that follow. That count includes each snapshot's price-level lines, which belong to the snapshot and are not events of their own.
- After each event, print one line: usually the top-of-book line, and from Part 3 on sometimes a
GAPline. - There are three parts, each adding behavior on top of the previous one. One solution must pass all eleven tests.
| Event | What it does |
|---|---|
SNAPSHOT seq nb na + nb bid lines + na ask lines | Replace the whole book (Part 1) |
QUERY | Change nothing (Part 1) |
DELTA seq side price size | Change one level (Part 2), or be rejected (Part 3) |
Part 1: Snapshots and queries
SNAPSHOT seq nb nais followed bynblines ofB price sizeand thennalines ofA price size.- It replaces the entire book with those levels and records
seqas the last applied sequence number. - Print the top-of-book line after every snapshot and every query. A query before any snapshot reports the empty book.
Output
BID price size | ASK price size
price sizeis the best level on that side.- An empty side prints as
NONE 0.
Limits
- Prices are positive integers and sizes are non-negative integers, both within the signed 32-bit range.
- Sequence numbers are non-negative integers below 10^18.
- Snapshot levels have positive sizes and no repeated price on a side.
- The input has at most 4 × 10^5 lines, and every
DELTAcomes after at least oneSNAPSHOT.
Tests 1–4 cover this part. They contain no DELTA lines.
Part 2: Apply incremental updates
DELTA seq side price size changes one level, where side is B or A. When seq is exactly one greater than the last applied sequence number:
- Set the level at
priceon that side tosize, or remove the level whensizeis 0. Removing a level that is not in the book changes nothing. - Record
seqas the last applied sequence number. - Print the top-of-book line.
A QUERY does not use up a sequence number.
Tests 5–7 cover this part. In them, every delta carries the next sequence number.
Part 3: Detect gaps and recover
Deltas can now arrive out of turn. The feed is either trusted or gapped.
- A delta is applied only when the feed is trusted and its
seqequals the last applied sequence number plus one. - Any other delta is rejected: print
GAP expected gotand put the feed in the gapped state.expected= the last applied sequence number plus one.got= the delta'sseq.
- A delta whose
seqis at or below the last applied one, such as a duplicate datagram, is rejected the same way. That keeps the handler to one rule; a production handler drops such a delta as stale instead (see On the hot path). - A rejected delta changes neither the book nor the last applied sequence number, so
expectedstays where the gap began. - While the feed is gapped, every delta is rejected, including one whose
seqequalsexpected. The handler has already discarded at least one later delta, so applying the missing one would not bring the book back in step. QUERYprints the last trusted book.- Any
SNAPSHOTreplaces the book, records itsseq, and returns the feed to the trusted state.
Tests 8–11 cover this part.
The example below uses all three parts: deltas from Part 2 and a gap from Part 3.
8 SNAPSHOT 10 1 1 B 100 5 A 102 3 DELTA 11 B 101 2 DELTA 13 A 102 0 QUERY SNAPSHOT 14 0 1 A 103 4
BID 100 5 | ASK 102 3 BID 101 2 | ASK 102 3 GAP 12 13 BID 101 2 | ASK 102 3 BID NONE 0 | ASK 103 4
The five events span eight lines, because each snapshot's level lines count toward the first line.
The first snapshot sets one bid level, 5 shares at 100, and one ask level, 3 shares at 102, and records sequence 10.
Delta 11 carries the next sequence number, so a bid level of 2 shares at 101 is added and becomes the best bid.
Delta 13 skips 12, so it is rejected with GAP 12 13, and the ask level at 102 stays in the book. The query prints the last trusted book.
The second snapshot replaces both sides, leaving the bid side empty. The feed is trusted again with 14 as the last applied sequence number.