The Runtime Theory
easyReference#order-book#matching

Match Orders from a Market Data Feed

Given a sequence of market orders and an order book state, determine which orders fill at which prices.

The Runtime Theory Team1 min read
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inOrder book asks: 100.05 × 30, 100.06 × 200. Market buy order for 50 shares.

outFills 30 at 100.05 and 20 at 100.06. Average fill price = 100.054.

inOrder book bids: 100.03 × 100, 100.02 × 50. Market sell order for 80 shares.

outFills 80 at 100.03. All 80 shares fill at the top bid.

inOrder book asks: 100.05 × 30, 100.10 × 200. Market buy order for 10 shares.

outFills 10 at 100.05. Remaining asks: 100.05 × 20, 100.10 × 200.

Match Orders from a Market Data Feed

You receive a market data feed showing the current order book state. You must determine which price levels a market order will hit and calculate the resulting fills.

Order Book Basics

The ask side (sell orders) is sorted from lowest to highest price. The bid side (buy orders) is sorted from highest to lowest price.

A market buy order sweeps up through asks (lowest ask first). A market sell order sweeps down through bids (highest bid first).

Example

plaintext
Asks:          Bids:
100.10 × 200   100.03 × 100
100.06 × 200   100.02 × 50
100.05 × 30    100.01 × 75

A market buy of 50 shares:

  • Fills 30 shares at 100.05 (all of level 1)
  • Fills 20 shares at 100.06 (partial fill of level 2)
  • Remaining: 180 shares at 100.06

Key Concepts

  • FIFO matching: orders at the same price level are filled in time order.
  • Partial fills: large orders may consume multiple price levels.
  • Slippage: the difference between expected and actual fill price.

One dispatch a week

The trace behind each problem, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.

Not started

Sign in to save your learning progress.

Sign in to save