Design an order matching engine
Hard40 minFree, no account
Price-time priority, partial fills, and an invariant that must hold after every single event.
The question
Design the in-memory order book for one instrument: limit and market orders, cancellations, and matching.
Matching must be deterministic and follow price-time priority.
Functional
- Place a limit order; it matches against the opposite side or rests.
- Market orders execute against the best available prices.
- Cancel a resting order by id.
- Emit a trade for every match, with both order ids and the executed price.
Non-functional
- Best bid and best ask in O(1).
- Cancel by id without scanning the book.
- The same input sequence must always produce the same trades.
40:00Commit to an answer before you open the solution. Reading it first teaches you to recognise good answers, which is not the skill being tested.
Stuck?
0 of 3 hints takenThe worked solution
written by a person · not a gradeScore yourself
0 of 5 marked- Two-level structure giving O(1) best price and FIFO within a level25
- O(1) cancel via an id index15
- Matched at the resting order's price25
- Partial fills preserve time priority15
- Stated the non-crossing invariant and kept matching deterministic20
We run no AI here and nothing on this page grades you. The score is yours, and the useful number is the one you get on the same problem a month from now, cold.
kept in this browser only