Match Limit Orders in a Price-Time Priority Book
Last updated: November 29, 2026
Quick Overview
Implement a single-symbol limit order book that accepts buy and sell limit orders and cancels, matches crossing orders at the resting order's price with price-time priority, and reports fills and the current best bid and ask.
Akuna Capital
November 29, 20260
0
0 solved
Implement a single-symbol limit order book that accepts buy and sell limit orders and cancels, matches crossing orders at the resting order's price with price-time priority, and reports fills and the current best bid and ask.
Akuna Capital is an options market maker, so engineers on trading and exchange-simulation teams are expected to know how a matching engine behaves. The interviewer checks that you get priority rules, partial fills and cancels right, and that you pick data structures with clear complexity rather than scanning the whole book.
What the Interviewer Expects
- State matching rules: trade at the resting price, best price first, earliest order first at a price
- Use heaps (or sorted price levels) for both sides with O(log n) insert and match
- Handle partial fills on both the aggressor and the resting order
- Support cancel without an O(n) heap search, for example lazy deletion
- Return fills as (aggressor, resting, price, qty) so results are testable
- Discuss how a production C++ book would differ (price-level arrays, intrusive lists)
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 order modify, and when does a modify lose time priority?
- How would you add market orders and immediate-or-cancel orders?
- If prices are ticks in a narrow band, what structure beats a heap?
- How do you produce top-of-book and depth updates for downstream consumers?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Orders are (id, side, price, qty). A buy crosses when its price is at or above the best ask, a sell when at or below the best bid. Trades print at the resting order's price. Within a price, the ea...
High-Level Design
Keep two heaps: bids as (-price, seq, id) and asks as (price, seq, id), where seq is an arrival counter that breaks ties by time. A dict orders[id] = [side, price, remaining] is the source of ...