Implement a Thread-Safe Bounded Blocking Queue for Order Events
Last updated: November 29, 2026
Quick Overview
Implement a bounded FIFO queue used between order-intake threads and fulfillment workers: put() blocks when the queue is full, take() blocks when it is empty, both support an optional timeout, and close() wakes all waiters so the system can shut down cleanly.
Amazon
November 29, 20260
0
0 solved
Implement a bounded FIFO queue used between order-intake threads and fulfillment workers: put() blocks when the queue is full, take() blocks when it is empty, both support an optional timeout, and close() wakes all waiters so the system can shut down cleanly.
A concurrency fundamentals question in the style of Amazon onsite rounds for backend teams. The interviewer checks correct use of a lock with condition variables, while-loop rechecks against spurious wakeups, timeout accounting, and a shutdown story that does not lose or hang on items.
What the Interviewer Expects
- Use one lock with two conditions (not_full, not_empty)
- Recheck predicates in a while loop
- Handle timeouts by tracking a deadline, not resetting per wakeup
- Notify the right condition after each operation
- Define close semantics: reject new puts, drain remaining items, then signal end
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 make it lock-free for a single producer and single consumer?
- How does this compare to SQS between services?
- How would you add priority lanes for Prime orders?
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 >= 1. put(item, timeout=None) returns True on success, False on timeout, raises Closed if the queue is closed. take(timeout=None) returns an item, raises TimeoutError on timeout, and raises C...
High-Level Design
- A
dequefor storage, onethreading.Lock, and twoConditionobjects sharing that lock. put: while full and open, wait onnot_fullwith remaining time; then append andnotifyone waiter ...