Build a Limit Order Book With Price-Time Priority Matching
Last updated: November 29, 2026
Quick Overview
Implement an in-memory order book for one trading pair that accepts buy and sell limit orders, matches them against resting orders by best price then arrival time, and supports cancels. Each placement returns the trades it produced.
Coinbase
November 29, 20260
0
0 solved
Implement an in-memory order book for one trading pair that accepts buy and sell limit orders, matches them against resting orders by best price then arrival time, and supports cancels. Each placement returns the trades it produced.
Coinbase coding rounds favor practical, multi-part problems over puzzles, and an exchange matching core is close to what Coinbase Exchange and Advanced Trade run. The interviewer checks that you pick the right structures for best-price lookup, handle partial fills correctly and keep cancels cheap.
What the Interviewer Expects
- Clarify integer prices (ticks) and quantities instead of floats
- Match at the resting (maker) order's price, best price first, then FIFO within a level
- Handle partial fills on both sides and leave the remainder resting
- Make cancel O(1) or amortized cheap via lazy deletion
- State complexity per operation and test with a short scripted scenario
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support market orders and immediate-or-cancel orders?
- How would you return an L2 snapshot (aggregated quantity per price level) efficiently?
- What changes if you must make the engine deterministic and replayable from an event log?
- How would you add self-trade prevention for orders from the same account?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
One pair, limit orders only. place(id, side, price, qty) returns a list of (taker, maker, price, qty) trades; cancel(id) removes a resting order; best_bid_ask() returns the top of book. Prices...
High-Level Design
Two heaps: bids keyed by (-price, seq) and asks by (price, seq), where seq is a global arrival counter, so the heap top is always best price and then earliest arrival. A dict live maps id to `...