Build a Price Time Priority Limit Order Book
Last updated: November 29, 2026
Quick Overview
Implement an order book for one equity that accepts limit buy and sell orders and cancels, matches incoming orders against resting ones by best price then earliest arrival, and returns the trades produced.
Bank of America
November 29, 20260
0
0 solved
Implement an order book for one equity that accepts limit buy and sell orders and cancels, matches incoming orders against resting ones by best price then earliest arrival, and returns the trades produced.
Fits an onsite for Global Markets electronic trading technology, where matching and internalization logic must be exact and fast. The interviewer checks data structure choice, correct partial fills, execution at the resting order's price, and an efficient cancel.
What the Interviewer Expects
- Max heap for bids and min heap for asks keyed by price then sequence
- Execute at the resting order's price
- Handle partial fills on both sides
- Cancel in O(1) with lazy deletion
- Explain why integer ticks are used instead of float prices
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 support market orders and immediate-or-cancel?
- How would you get O(1) best-price access with a fixed tick range?
- How would you make the book recoverable after a crash?
- What changes for an order amend that reduces size versus one that changes price?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Operations: submit(order_id, side, price, qty) returns a list of trades (incoming id, resting id, price, qty) and rests any remainder; cancel(order_id) removes a resting order. Prices are integer ...
High-Level Design
Two heaps: bids keyed by (-price, seq), asks by (price, seq). A dictionary holds live orders and their remaining quantity. Cancel just deletes from the dictionary; stale heap entries are skipped when ...