Price Time Priority Matching Engine With Self Trade Prevention

Last updated: November 29, 2026

Quick Overview

Implement a limit order book for one trading pair that matches incoming limit orders by price then time priority, rests any unfilled remainder, supports cancel, and prevents an account from trading against its own resting order by cancelling the resting order.

Gemini
Coding & Algorithms
Software Engineer
Gemini
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Implement a limit order book for one trading pair that matches incoming limit orders by price then time priority, rests any unfilled remainder, supports cancel, and prevents an account from trading against its own resting order by cancelling the resting order.

This is the heart of an exchange like Gemini, whose trading system is built for sub-millisecond latency. In an onsite coding round the interviewer looks for correct priority rules, efficient best-price lookup, O(1) cancels, and awareness of exchange rules such as self-trade prevention.

What the Interviewer Expects
  • State price-time priority precisely: better price first, then earlier arrival at the same price
  • Fill at the resting (maker) price, not the taker's limit
  • Choose data structures for O(log P) best price and O(1) cancel
  • Implement self-trade prevention and explain the alternatives (cancel newest, cancel oldest, cancel both)
  • Handle partial fills on both sides and resting of remainders
  • Walk through a multi-level sweep by hand
Key Topics to Cover
Order Book
Heap
Lazy Deletion
Queue
Self Trade Prevention
Low Latency
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 market orders and immediate-or-cancel?
  • How would you make the engine deterministic and replayable from a journal for failover?
  • Why might a production engine use arrays indexed by integer tick instead of heaps?
  • How would you add maker-or-cancel (post-only) orders?
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

Single symbol, integer prices in ticks and integer quantities. submit(oid, account, side, price, qty) returns a list of fills (maker_id, taker_id, price, qty). Matching: a buy crosses while best a...

High-Level Design

Two heaps of price levels (max for bids via negation, min for asks), a map from price to a FIFO deque of order ids for time priority, and a map orders holding live state. Cancel just deletes from `o...


Submit Your Answer
Markdown supported

Related Questions