Build a Thread-Safe Bounded Queue for Market Data
Last updated: November 29, 2026
Quick Overview
Implement a fixed-capacity queue shared by producer and consumer threads, where put blocks while full and get blocks while empty, with optional timeouts and a close method that lets consumers drain and exit cleanly.
SIG
November 29, 20260
0
0 solved
Implement a fixed-capacity queue shared by producer and consumer threads, where put blocks while full and get blocks while empty, with optional timeouts and a close method that lets consumers drain and exit cleanly.
SIG's trading systems pass market data and order events between threads, where an unbounded queue hides a slow consumer until memory runs out. This onsite question checks condition variables, spurious-wakeup-safe loops, timeouts, and a shutdown protocol.
What the Interviewer Expects
- Use one lock with two condition variables, not_full and not_empty
- Wait inside while loops, never a single if
- Track a deadline so repeated wakeups do not extend the timeout
- Notify the opposite condition after each put or get
- Define close semantics: wake all waiters, let consumers drain
- Discuss backpressure vs. dropping data for market feeds
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- For a market data feed, when would you rather drop the oldest update than block the producer?
- How would you build a lock-free single-producer single-consumer ring buffer?
- What goes wrong if you use notify instead of notify_all in close?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
put(item, timeout=None) returns True when enqueued and False on timeout. get(timeout=None) returns an item, raises TimeoutError on timeout, and returns None once the queue is closed and empty....
High-Level Design
A deque holds items under a single Lock. Two Condition objects share that lock so producers and consumers wait on different signals. Each wait sits in a while loop that re-checks the predicate...