Design an Order Matching Engine
📈 What is an order matching engine? It is the heart of a stock exchange. Traders send orders like "buy 100 shares of AAPL at up to 150" or "sell 50 shares at the best price you can get". The engine keeps the orders that cannot trade yet in an order book, and when a new order arrives it pairs it with the best waiting orders on the other side and reports the trades.
This problem is a favorite at trading firms and fintech companies, and it shows up in general LLD rounds too. The rules fit on one page, but getting them exactly right takes care: who trades first, at what price, what happens to the leftover quantity. It also rewards picking the right data structures, which is rarer in LLD interviews than in coding rounds.
The prompt comes in a few shapes that are really one design. Some interviewers ask you to "design an order book". Some say "implement price-time priority". Many split it in two parts: first "here is a list of orders, return all the trades" (batch), then "now orders arrive one at a time; report each trade as it happens" (streaming). We build one engine that matches each order the moment it arrives. A batch is then just a loop over that engine, and we show in the Implementation why the obvious batch shortcut (sort everything, then pair) gives wrong answers.
Requirements
"Design the matching engine for a stock exchange. Traders send buy and sell orders. Match them and report the trades."
Every word in that prompt hides a rule. Ask before you draw anything.
Clarifying Questions
You: What kinds of orders do we support?
Interviewer: Limit orders and market orders. A limit order has a worst acceptable price: a buyer pays at most that much, a seller gets at least that much. A market order has no price and takes whatever is available.
You: When several waiting orders could trade with a new one, which goes first?
Interviewer: Price-time priority. The best price first: the cheapest seller for a buyer, the highest bidder for a seller. Among orders at the same price, the one that arrived first.
You: At what price does a trade happen? The buyer's price, the seller's, or the middle?
Interviewer: The price of the order that was already waiting in the book.
You: What if the quantities don't match?
Interviewer: Trade the smaller quantity. An incoming order can trade with many waiting orders. Whatever is left of a limit order waits in the book. Whatever is left of a market order is dropped; a market order never waits.
You: One stock or many? And can traders cancel?
Interviewer: Many stocks, each with its own book. Traders can cancel a waiting order by its id. Changing an order is out of scope for now.
You: Do you want the trades for a whole list of orders at once, or as each order comes in?
Interviewer: Start with a list of orders and give me all the trades. Then we'll talk about orders arriving one at a time, where each trade must be reported immediately.
You: Do I need timestamps to decide who arrived first?
Interviewer: No. The order in which the engine receives them is the arrival order.
The third answer is the one candidates most often get wrong. If a seller is waiting at 100 and a buyer arrives willing to pay 105, the trade happens at 100. The waiting order set the price; the newcomer simply gets a good deal. This rule is what makes resting in the book safe: you never trade at a price worse than the one you posted.
💡 Interview tip: Ask the batch-or-streaming question even if the interviewer does not split the problem. If they say "a list of orders", confirm that the list is in arrival order. That one fact tells you the batch is a replay of a stream, and you only have to build one thing.
Final Requirements
- The engine accepts limit orders (side, price, quantity) and market orders (side, quantity), each with a unique id and a stock symbol. Each symbol has its own book.
- A limit buy trades with sell orders priced at or below its price; a limit sell trades with buy orders priced at or above its price. A market order trades with any price.
- Price-time priority: an incoming order trades with the best price first; at the same price, the earliest order first.
- Every trade happens at the resting order's price, for the smaller of the two remaining quantities. An order can trade many times.
- After matching, the rest of a limit order waits in the book. The rest of a market order is dropped.
- A trader can cancel a waiting order. Each order has a status: open, partially filled, filled or cancelled.
- Anyone can read the best bid, the best ask, and the waiting volume at a price.
- Orders are processed one at a time in arrival order. A list of orders (batch) gives the same trades as feeding them one by one (streaming).
- Invalid orders (reused id, quantity of zero or less, a limit price of zero or less) are rejected and change nothing.
Out of scope
- Traders, accounts and balances: the exchange trusts the brokers that send it orders (see /low-level-design/stock-brokerage for that side).
- Order changes, stop orders, fill-or-kill and other order types (we come back to these in Extensibility).
- Network protocols, persistence and market data feeds.
- Several threads calling the engine at once (also an Extensibility question).