Price-Level Order Book With Add, Reduce and Best Quote
Last updated: November 29, 2026
Quick Overview
Design an order book that supports adding a limit order, reducing or cancelling it by id, and returning the best bid and best ask with their total quantity, all efficiently.
Voleon
November 29, 20260
0
0 solved
Design an order book that supports adding a limit order, reducing or cancelling it by id, and returning the best bid and best ask with their total quantity, all efficiently.
Voleon builds and replays limit order books from exchange feeds to simulate execution, so this design question checks practical data-structure judgment. The interviewer wants per-level aggregation, fast best-price lookup, a correct cancellation path, and an honest discussion of heap versus sorted container trade-offs.
What the Interviewer Expects
- Store orders by id for O(1) lookup on cancel
- Aggregate quantity per price level per side
- Find best bid and ask quickly with heaps and lazy deletion
- Remove empty levels and skip stale heap entries
- State the complexity of every operation
- Validate bad ids and nonpositive quantities
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 iterating the top N levels in price order?
- How would you add matching when an incoming order crosses the spread?
- How would you preserve time priority within a level?
- Which structure would you choose in C++ for a production feed handler?
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), reduce(id, qty) for partial fills or partial cancels, cancel(id), best_bid() and best_ask() returning (price, total_qty) or None. Updates are far mor...
High-Level Design
Three pieces. A dict orders from id to [side, price, remaining]. A dict per side from price to aggregate quantity. A heap per side of prices (negated for bids) to find the best level. Heaps cannot...