Price-Time Priority Matching Book With Lazy Cancels
Last updated: November 29, 2026
Quick Overview
Implement a single-instrument limit order book that supports add (which matches against the opposite side by price-time priority and rests any remainder) and cancel by order id, and can report the best bid and offer.
Virtu Financial
November 29, 20260
0
0 solved
Implement a single-instrument limit order book that supports add (which matches against the opposite side by price-time priority and rests any remainder) and cancel by order id, and can report the best bid and offer.
Virtu operates crossing and ATS businesses (MatchIt, POSIT) and its market makers simulate exchange books constantly, so a matching engine is a core onsite exercise. The interviewer wants correct priority rules, partial fills, O(log P) price-level access and a cancel that does not scan a queue.
What the Interviewer Expects
- Match at the resting order's price, not the incoming price
- Preserve FIFO within a price level and partial fills on both sides
- Make cancel O(1) using lazy deletion instead of scanning the level
- Keep best-price lookup efficient with heaps or a sorted structure
- Clean up empty levels so stale prices never appear in the BBO
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 modify-quantity that keeps or loses priority?
- How would you implement this in C++ with no heap allocation on the hot path?
- How would you add IOC and post-only order types?
- How would you make the engine 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
add(oid, side, price, qty) returns fills (resting_id, incoming_id, price, qty); unmatched quantity rests. cancel(oid) removes a resting order. bbo() returns (best_bid, best_ask). Prices are ...
High-Level Design
Per side: a dict price to deque of order ids, plus a heap of prices (bids negated). A global dict holds [side, price, remaining] for live orders. Cancel just deletes from that dict; the id stays in ...