Implement a Thread-Safe Bounded Blocking Queue
Last updated: November 29, 2026
Quick Overview
Implement a fixed-capacity queue where put blocks while the queue is full and get blocks while it is empty, safe for many producer and consumer threads, with optional timeouts.
Microsoft
November 29, 20260
0
0 solved
Implement a fixed-capacity queue where put blocks while the queue is full and get blocks while it is empty, safe for many producer and consumer threads, with optional timeouts.
Producer-consumer buffers sit inside Windows I/O completion handling, Azure SDK pipelines, and logging agents, and Microsoft interviewers ask candidates to build one from primitives. They check correct use of condition variables (waiting in a loop, the right notify), avoidance of lost wakeups, and clean timeout semantics.
What the Interviewer Expects
- Protects shared state with one lock
- Uses two conditions (not full, not empty) on the same lock
- Waits in a predicate loop to handle spurious wakeups
- Notifies the opposite condition after each change
- Supports timeouts without leaving state inconsistent
- Explains why a plain lock or busy waiting is wrong
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
- Add a close() method so blocked consumers wake up and stop cleanly.
- Why is notify() enough here, and when would you need notify_all()?
- How would you build this lock-free with a ring buffer for a single producer and single consumer?
- How would you add batching so a consumer can take up to n items at once?
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) blocks until there is room, then enqueues; get(timeout=None) blocks until an item exists, then dequeues in FIFO order. Both raise TimeoutError if the timeout elapses. Any...
High-Level Design
All state (the deque) is guarded by a single mutex. Two condition variables share that mutex: producers wait on not_full, consumers wait on not_empty. After a put, signal not_empty; after a get,...