Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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. lines holds 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 GAP line.
  • There are three parts, each adding behavior on top of the previous one. One solution must pass all eleven tests.
EventWhat it does
SNAPSHOT seq nb na + nb bid lines + na ask linesReplace the whole book (Part 1)
QUERYChange nothing (Part 1)
DELTA seq side price sizeChange one level (Part 2), or be rejected (Part 3)

Part 1: Snapshots and queries

  1. SNAPSHOT seq nb na is followed by nb lines of B price size and then na lines of A price size.
  2. It replaces the entire book with those levels and records seq as the last applied sequence number.
  3. 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 size is 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 DELTA comes after at least one SNAPSHOT.

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:

  1. Set the level at price on that side to size, or remove the level when size is 0. Removing a level that is not in the book changes nothing.
  2. Record seq as the last applied sequence number.
  3. 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.

  1. A delta is applied only when the feed is trusted and its seq equals the last applied sequence number plus one.
  2. Any other delta is rejected: print GAP expected got and put the feed in the gapped state.
    • expected = the last applied sequence number plus one.
    • got = the delta's seq.
  3. A delta whose seq is 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).
  4. A rejected delta changes neither the book nor the last applied sequence number, so expected stays where the gap began.
  5. While the feed is gapped, every delta is rejected, including one whose seq equals expected. The handler has already discarded at least one later delta, so applying the missing one would not bring the book back in step.
  6. QUERY prints the last trusted book.
  7. Any SNAPSHOT replaces the book, records its seq, 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.

Example
Input
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
Output
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
Explanation

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.