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
Coding & Algorithms
Software Engineer
Imc
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

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
order book
price-time priority
heaps
lazy deletion
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 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 Problems
Sample 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 a deque of [order_id, qty] entries in arrival order.
  • bid_heap (negated prices) and ask_heap for best price lookup.
  • orders: id to the mutable entry, s...

Submit Your Answer
Markdown supported

Related Questions