Quant Developer: Order book design
Scope an order book
About 45 minutes, start to finish.
Open withBefore any code, I'd like to agree what the book has to do, how fast, and what's out of scope today.
The clock
Stage by stage
Close each stage out loud, so they always know where you are.
- 1Operations and their speed8 min
Those are the operations and the time each message gets. Now the data structures.
- 2Data structures12 min
That's the structure for price levels, orders and cancels. Next, the matching rules.
- 3Matching rules8 min
Those are the rules, worked on an example. Now what goes out on the feed.
- 4The feed9 min
That's what we publish and how a client recovers. Last, how I'd test it.
- 5Testing8 min
That's how I'd come to trust it. Happy to write the core next.
How people lose this round
I'd start with a lock-free concurrent tree.
A clever structure before the requirements worries an interviewer. One thread per book is usually simpler and faster.To cancel, I'd scan the level for the order id.
Cancels are a large share of messages, and a busy level holds thousands of orders. Keep an index from id to order.The buyer pays their limit price.
Trades print at the resting orders' prices. A buy that sweeps two levels pays each level's price.I'd measure the average latency.
The rare slow message is what hurts, and an average hides it. Give the 99.9th percentile and the maximum.Unit tests for each function will cover it.
Order book bugs live in sequences of messages. Check invariants and compare against a simple reference on random streams.
A worked answer
The notes in the margin mark where the candidate asked, put a number on it, took a hint or caught a slip.
- Interviewer
Design the order book and matching engine for one stock.
- You
Before any code, let me agree the scope. I'm assuming limit orders, market orders and cancels, one stock, one thread, and price-time priority. Out of scope today: auctions, amending an order, and risk checks. Is that fair?
States the scope and asks to confirm.
The rest of this answer comes with Pro.
Pro opens every worked answer, margin notes and all.