Price-Time Priority Limit Order Book with Partial Fills and Cancels
Last updated: November 29, 2026
Quick Overview
Implement a single-instrument limit order book that accepts buy and sell limit orders, matches them with price-time priority at the resting order's price, supports partial fills and cancels, and reports best bid and ask.
UBS
November 29, 20260
0
0 solved
Implement a single-instrument limit order book that accepts buy and sell limit orders, matches them with price-time priority at the resting order's price, supports partial fills and cancels, and reports best bid and ask.
Electronic trading engineers in the Investment Bank build order management and internal crossing logic, so a matching engine exercise is a natural onsite coding round. The interviewer wants clean data structure choices, exact matching semantics, and a sensible approach to cancels that does not make every operation linear.
What the Interviewer Expects
- Clarify trade price convention and what happens to unfilled quantity
- Use two priority queues keyed by price then arrival sequence
- Loop over crossing resting orders producing partial fills
- Support cancel in better than linear time, for example lazy deletion
- Walk through a test with several fills and a cancel
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 add market orders and immediate-or-cancel orders?
- How would you make cancel and modify strictly O(log n) without lazy deletion garbage?
- What changes if you need to publish an aggregated depth-of-book feed per price level?
- How would you make the engine deterministic for replay after a crash?
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: id, side (B or S), limit price, quantity. An incoming order matches against the opposite side while prices cross (buy price >= best ask, or sell price <= best bid). Among equal prices, earlier...
High-Level Design
Bids live in a max-heap and asks in a min-heap. Each entry is (price key, sequence number, order), where the key is -price for bids, so tuple comparison gives price priority then time priority fro...