Concurrent Stock Positions
What this preview is
Concurrent Stock Positions is a hard quant coding problem on concurrency in Cpp.
- Difficulty
- Hard
- Topic
- Concurrency
- Discipline
- Quant development
- Language
- Cpp
Concurrent position tracking under per-symbol lock contention
This is a hard C++ concurrency problem typical of high-frequency trading systems, where correctness and latency both matter. It tests whether you can design a thread-safe data structure that avoids global serialization bottlenecks while guaranteeing atomic read-modify-write semantics on individual positions.
The core challenge is enforcing that a sell operation—checking available shares and decrementing them—cannot be split across two separate steps where another thread intervenes. A naive global lock solves atomicity but creates a throughput cliff: unrelated trades in different symbols queue behind each other, destroying concurrency. The key insight is that trades in different symbols are independent, so each symbol deserves its own synchronization primitive rather than a shared one. Combined with the constraint that at most 6500 symbols exist for the program's lifetime, this suggests pre-allocating per-symbol locks or atomic counters and indexing them by symbol identity.
Strong solutions address several design decisions: how to map unbounded string symbols to a bounded set of locks, whether to use mutexes or atomics for the hot path, how to handle the all-or-nothing atomicity of sell, and whether the first trade on a new symbol requires any special coordination. The implementation must scale to arbitrary thread counts and arbitrary call overlaps without false contention in the common case.
- Per-resource locking vs. global locks and throughput
- Atomic compare-and-swap for lock-free read-modify-write
- Hash-based symbol-to-lock mapping under a bounded keyspace
- All-or-nothing transaction semantics without rollback
Unlock full access to getcracked
Join to unlock this problem, detailed solutions, and our complete library of quant finance interview prep.