Thread-Safe Bounded Blocking Queue for Event Producers
Last updated: November 29, 2026
Quick Overview
Implement a bounded blocking queue with put(item, timeout) and get(timeout) that is safe for many producer and consumer threads: put blocks while the queue is full, get blocks while it is empty, and both give up after the timeout.
0
0
0 solved
Implement a bounded blocking queue with put(item, timeout) and get(timeout) that is safe for many producer and consumer threads: put blocks while the queue is full, get blocks while it is empty, and both give up after the timeout.
LinkedIn created Kafka, and buffering events between producers and a network sender is a daily concern in its client libraries. This concurrency round checks condition variables, the while-loop wait pattern, timeouts, and reasoning about lost wakeups.
What the Interviewer Expects
- Uses one lock with two condition variables (not_full, not_empty)
- Waits in a while loop to handle spurious wakeups and stolen items
- Notifies the opposite condition after each state change
- Implements timeouts by tracking a deadline across repeated waits
- Explains why notify instead of notify_all is safe here, or chooses notify_all with reasoning
- Writes a test with multiple threads that checks no items are lost or duplicated
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
- How would you add a close() so blocked threads wake up and exit?
- How would you batch gets, like the Kafka producer's linger.ms?
- How does a lock-free ring buffer compare for a single producer and single consumer?
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
Capacity is fixed at construction. put(item, timeout=None) returns True on success, False on timeout. get(timeout=None) returns the item or raises TimeoutError. FIFO order. Many threads on both ...
High-Level Design
A deque holds items, guarded by one threading.Lock. Two Condition objects share that lock: producers wait on not_full, consumers on not_empty. The wait sits inside while because a woken th...