Limit Order Book With Price Time Priority Matching
Last updated: November 29, 2026
Quick Overview
Implement a single instrument limit order book supporting add and cancel. An incoming limit order matches against the best opposite prices at the resting order's price, oldest first at each level, and any remainder rests. Expose best bid and best ask.
DRW
November 29, 20260
0
0 solved
Implement a single instrument limit order book supporting add and cancel. An incoming limit order matches against the best opposite prices at the resting order's price, oldest first at each level, and any remainder rests. Expose best bid and best ask.
An order book is the canonical DRW coding question because it touches the core of what the firm does: matching, priority, and fast lookups. The interviewer probes data structure choice for price levels, correctness of partial fills and cancels, and how you would make it fast enough for real market data.
What the Interviewer Expects
- Chooses a structure giving fast best price access and FIFO per level
- Matches at the resting price and respects time priority
- Handles partial fills on both sides
- Supports O(1) cancel via lazy deletion or intrusive lists
- Discusses complexity and a production layout (arrays indexed by tick)
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 make cancel truly O(1) and avoid lazy garbage?
- Add order modify: which changes keep priority and which lose it?
- How would you support market orders and immediate-or-cancel?
- How would you lay this out in memory for a C++ implementation handling millions of messages per second?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Operations: add(id, side, price, qty) returning fills (resting_id, aggressor_id, price, qty), cancel(id) returning whether the order was live, best_bid(), best_ask(). Prices are integer tick...
High-Level Design
Per side: a dict from price to a deque of order ids (time priority), and a heap of prices (bids negated) for best price. A dict from id to [side, price, remaining] holds live orders. Cancel just del...