Match Limit Orders in a Crypto Order Book With Cancellations
Last updated: November 29, 2026
Quick Overview
Implement an order book for one trading pair that accepts buy and sell limit orders, matches them at price-time priority with partial fills, supports cancelling resting orders, and reports the best bid and ask.
Revolut
November 29, 20260
0
0 solved
Implement an order book for one trading pair that accepts buy and sell limit orders, matches them at price-time priority with partial fills, supports cancelling resting orders, and reports the best bid and ask.
Revolut runs crypto and stock trading for retail users, and its onsite coding rounds often ask for a small working engine rather than a single function. The interviewer checks price-time priority, correct partial fills at the resting order's price, and an efficient cancel without scanning the heap.
What the Interviewer Expects
- Keep bids in a max-heap and asks in a min-heap, ties broken by arrival sequence
- Fill at the resting order's price and support partial fills on both sides
- Implement cancel in O(1) using lazy deletion instead of removing from the heap
- Rest any unfilled remainder of an incoming order on its own side
- Discuss complexity and how the design changes with many orders per price level
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 add market orders and immediate-or-cancel orders?
- Why might a real exchange use a sorted map of price levels with FIFO queues instead of heaps?
- How do you prevent a user's buy order from matching their own sell order?
- How would you make matching deterministic and recoverable after a crash?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
place(order_id, side, price, qty) returns trades as (resting_id, incoming_id, price, qty). A buy matches asks priced at or below its limit, cheapest first, oldest first at equal price; sells mirro...
High-Level Design
Two heaps: bids as (-price, seq, id) and asks as (price, seq, id), so the top is always the best price and oldest order. A dict orders holds live remaining quantity and is the source of truth. C...