Build a Price-Time Priority Limit Order Book With Cancels
Last updated: November 29, 2026
Quick Overview
Implement a limit order book for one instrument that accepts buy and sell limit orders, matches them at the resting order's price under price-time priority, supports cancel by order ID, and returns the fills produced by each submission.
Citi
November 29, 20260
0
0 solved
Implement a limit order book for one instrument that accepts buy and sell limit orders, matches them at the resting order's price under price-time priority, supports cancel by order ID, and returns the fills produced by each submission.
Citi's markets technology runs electronic trading in FX, rates and equities (Citi Velocity among them), and matching logic is a common onsite exercise for those teams. The interviewer checks priority rules, partial fills, the cost of cancels, and whether the code stays correct when a cancelled order sits at the top of the book.
What the Interviewer Expects
- Bids as a max-heap and asks as a min-heap keyed by price then arrival sequence
- Match while the incoming price crosses the best opposite price
- Trade at the resting order's price and handle partial fills
- Cancel in O(1) with lazy deletion and skip dead entries at the top
- Rest any unfilled remainder
- Discuss heap vs sorted price levels with FIFO queues
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?
- Replace the heap with price levels of FIFO queues: what improves?
- How would you publish top-of-book and depth updates to market data consumers?
- How do you make matching deterministic for replay and audit?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
submit(id, side, price, qty) with side B or S returns fills (resting_id, incoming_id, price, qty). A buy matches asks priced at or below its limit, best (lowest) price first, and earliest arri...
High-Level Design
asks: min-heap of(price, seq, id);bids: min-heap of(-price, seq, id)so the top is the highest bid.seqfrom a counter gives time priority.orders: id to[side, price, remaining]....