Building a thread-safe order book snapshot
This medium-difficulty concurrency problem tests whether you can design a data structure that allows high-frequency mutations (adding and canceling orders) while guaranteeing consistent snapshots. It's representative of the kind of lock-and-state-management challenges that arise in real trading systems.
The core tension: mutating threads need to run concurrently for throughput, but a snapshot reader must see an atomic view with no torn reads or partial updates. The solution hinges on choosing the right synchronization primitive and understanding when to hold versus release locks. You'll need to manage order storage efficiently, track unique IDs, and maintain sorted output on read without blocking writers unnecessarily.
- Reader-writer lock patterns and their trade-offs
- Atomic counter generation for order IDs
- Data structure choice for fast insertion, deletion, and sorted retrieval
- Lock scoping and avoiding deadlock