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
Coding & Algorithms
Software Engineer
Virtu Financial
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
order book
heap
lazy deletion
price-time priority
matching engine
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported

Related Questions