Online Stock Brokerage

Trading Platform & Order Matching

Design a brokerage with an order book per stock: market, limit and stop orders matched by price-time priority, partial fills, cancels, and accounts whose cash and shares are reserved so nothing is spent twice.

Overview

A brokerage lets people buy and sell shares. At its center is an order book for each stock: the buy orders waiting at each price (bids) and the sell orders waiting at each price (asks). A new order is matched against the other side of the book; whatever does not fill either waits in the book (a limit order) or is dropped (a market order). Around the book sit accounts, which must hold enough cash or shares before an order is accepted.

The problem tests three things. First, the matching rule: best price first, and among orders at the same price, the oldest first (price-time priority, also called FIFO). Nasdaq's own book rule works this way, and also ranks displayed orders ahead of hidden ones at the same price. Second, the data structure that makes that rule cheap: a queue per price level and a sorted list of prices. Third, money safety: an account with 100 dollars must not place two 100-dollar orders, so cash and shares are reserved when an order is placed and released when it fills or is canceled.

A strong answer walks through one trade by hand, draws the book, keeps money in integer cents, and ends with a demo that checks partial fills, time priority, a cancel and a stop order.

Scoping the problem

Ask these questions first, because each answer adds or removes a rule.

  • Are we the exchange, or a broker sending orders to one? A broker routes orders and an exchange matches them. Build the matching core, and treat routing as a follow-up.
  • Which order types? Each type adds a rule. The usual answer is market, limit and sell stop.
  • Partial fills? Usually yes, so orders keep a filled count.
  • Does a market order's remainder wait in the book? This changes what happens when liquidity runs out. The usual answer is no: the remainder is canceled.
  • Short selling or margin? This changes the account checks. The usual answer is no: sell only shares you hold, and buy only with cash you have.
  • How are prices stored? Floats round badly, so use integer cents.
  • Many clients at once? Yes, and this decides the locking: one symbol's orders are processed one at a time.

Functional requirements

  • place(account, symbol, side, kind, qty, price, stop) accepts market, limit and sell stop orders
  • Orders match by price-time priority; a trade happens at the resting order's price; partial fills are allowed
  • A limit remainder rests in the book; a market remainder is canceled
  • A sell stop becomes a market order when the last trade price falls to its stop price
  • cancel(order_id) removes an open or partly filled order and releases its reservation
  • Buys need free cash and sells need free shares at placement, or the order is rejected
  • Every trade is sent to listeners (market data, audit log)

Non-functional requirements

  • Money in integer cents; no floating point in balances
  • Best price and matching the front order are O(1)
  • One symbol is processed one order at a time, so results are deterministic; different symbols run in parallel
  • No cash or share is created or lost: a threaded test checks the totals
  • Listeners are called outside the book lock, so a slow subscriber does not stall matching

Interview questions

  • Why is a trade priced at the resting order's price, and what does the incoming order gain?
  • Price-time priority is not the only rule. What else do exchanges use?
  • How do stop orders work, and what can go wrong?
  • How does this scale to many symbols and a crash-safe engine?
  • Why integer cents instead of floats?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio