Building a streaming order book with efficient imbalance queries
This hard Python coding problem tests whether you can maintain a live, sorted order book and answer multiple queries on demand — a core skill at market-making and prop-trading desks. The challenge is not the logic itself, but achieving the right data structure so that 500,000 updates and 200,000 queries run within reasonable time.
The key insight is that updates can occur at any price level, and queries ask for the top k levels by price (highest bids, lowest asks). Naive re-sorting on each query will time out. You'll need a data structure that keeps bid and ask sides sorted without rebuilding from scratch. You also need to handle level deletions (when quantity drops to zero) cleanly.
- Choosing the right container for fast insertion, deletion, and ordered traversal (e.g.
SortedDict or a balanced tree)
- Computing aggregate statistics (sum of quantities, volume-weighted average) over top-k slices
- Edge cases: empty sides, fewer than k levels, spread when book is incomplete