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
Software Engineering Fundamentals
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

0

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
Concurrency
Condition Variables
Producer Consumer
Locks
Graceful Shutdown
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. 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 Problems
Sample 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 deque for storage, one threading.Lock, and two Condition objects sharing that lock.
  • put: while full and open, wait on not_full with remaining time; then append and notify one waiter ...

Submit Your Answer
Markdown supported