Skip to main content
Abstract Algorithms Logo
Abstract Algorithms
โ† All Problemsยทlow level designMedium~25 min

Design a Thread-Safe Bounded Blocking Queue

Design and implement a thread-safe Bounded Blocking Queue with put(element) and get() methods. When the queue is full, producer threads calling put() must block until space becomes available. When the queue is empty, consumer threads calling get() must block until an element is added.

Requirements

  • โœ“Bounded capacity specified at construction time.
  • โœ“put(element): inserts element, blocking if full until space is available.
  • โœ“get(): retrieves and removes head element, blocking if empty until element is available.
  • โœ“Thread safety: safe under concurrent access by multiple producer and consumer threads.
  • โœ“FIFO ordering: elements are retrieved in the order they were inserted.

Constraints & Scale

  • โ€”No busy-waiting / polling loops (while true sleep). Must use condition synchronization.
  • โ€”Guard against spurious wakeups.
  • โ€”Prevent deadlocks between producers and consumers.
Understand
Guided mode

What is the difference between a mutex lock and a condition variable? Why is a single mutex insufficient without condition variables?

Context & Constraints โ€” Human Reviewed

A mutex provides mutual exclusion (only one thread enters critical section). Condition variables allow threads to release the mutex and sleep until signaled that state has changed. We need two conditions: notEmpty (consumers wait on) and notFull (producers wait on).

Trade-offs

  • โš–One lock + two condition variables: clean, thread-safe, prevents deadlocks.
  • โš–Two locks (head lock & tail lock like Java LinkedBlockingQueue): allows simultaneous enqueue and dequeue, boosting concurrency at cost of complex state tracking.

Concepts used in this stage