Design an order-matching engine
Akuna CapitalSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to design a high-throughput, low-latency in-memory limit order-matching engine, testing competencies in data structure selection, algorithmic complexity, deterministic concurrency, API design, and handling domain-specific constraints such as self-trade prevention.
Design and implement an in-memory order-matching engine for a limit order book. Requirements:
- Operations:
- placeOrder(orderId, side, price, quantity, type) where side ∈ {BUY, SELL} and type ∈ {LIMIT, MARKET}
- cancelOrder(orderId)
- topOfBook(): return best bid and best ask (price, totalQuantity)
- depth(k): return top k price levels on each side
- Matching rules: price–time priority; match immediately on arrival; allow partial fills; leave remainders on the book; market orders sweep the book until filled or empty; prevent self-trade.
- Performance targets: support up to 1e6 live orders and ~1e5 ops/sec; aim for O(log P) insert/cancel and O(
- top-of-book; avoid linear scans over orders.
- Concurrency: single-threaded matcher is acceptable; describe how you would extend to multi-threading without breaking determinism. Deliverables: specify data structures (e.g., price-indexed maps with FIFO queues), implement core APIs, analyze time/space complexity, and explain strategies to prevent timeouts on adversarial inputs.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.