Match Limit Orders With Price-Time Priority and Cancels
Last updated: November 29, 2026
Quick Overview
Implement a single-instrument limit order book that accepts add and cancel requests, matches incoming orders against resting liquidity using price-time priority, returns the generated trades and reports the best bid and ask.
Imc
November 29, 20260
0
0 solved
Implement a single-instrument limit order book that accepts add and cancel requests, matches incoming orders against resting liquidity using price-time priority, returns the generated trades and reports the best bid and ask.
IMC builds its own exchange simulators, backtesters and book builders from venue feeds, so engineers are expected to understand matching semantics precisely. The interviewer checks data structure choices for best-price access and FIFO queues, correct partial fills, and an efficient cancel path.
What the Interviewer Expects
- Keeps a FIFO queue per price level
- Gets best price in O(log P) or better
- Matches at the resting order's price, not the aggressor's
- Handles partial fills on both sides
- Supports O(1) cancel by order id
- Discusses lazy deletion versus eager removal
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 amendments that keep or lose queue priority?
- Prices are integer ticks in a narrow band. What structure beats a heap?
- How would you add immediate-or-cancel and post-only order types?
- How do you make the book deterministic for replay in a backtester?
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(order_id, side, price, qty) returns a list of trades (aggressor_id, resting_id, price, qty); cancel(order_id) returns whether it was live; top() returns (best_bid, best_ask)....
High-Level Design
bids,asks: dict from price to adequeof[order_id, qty]entries in arrival order.bid_heap(negated prices) andask_heapfor best price lookup.orders: id to the mutable entry, s...