Design a Bounded Blocking Queue
🚦 What is a bounded blocking queue? It is a queue that several threads share. Some threads (producers) put work in, others (consumers) take work out. It has a fixed size. A producer that finds it full waits until there is room, and a consumer that finds it empty waits until something arrives. Nobody spins, nobody drops work, and the queue never grows past its limit.
This is the classic producer-consumer problem, also called the bounded buffer, and it is the same design as LeetCode 1188 "Design Bounded Blocking Queue". All three ask for one class with enqueue and dequeue that block at the right moments. The real topic is condition variables: how a thread goes to sleep until some fact about shared state becomes true, and how another thread wakes it. We cover each of those in the requirements and the implementation below.
Interviewers like this problem because the class is tiny but the bugs are subtle. A version that looks right can lose items, go over capacity, or freeze with every thread asleep. You will be judged on whether you can explain why your version cannot do any of those things.
Requirements
"Implement a thread-safe queue with a fixed capacity. Producers should block when it is full, and consumers should block when it is empty."
That sentence leaves several real choices open. Here are the questions that change the code.
Clarifying Questions
You: When a producer calls enqueue on a full queue, should it wait, or fail right away?
Interviewer: Wait until there is room. We can talk about a version with a timeout later.
You: Is it first in, first out?
Interviewer: Yes. Items come out in the order they went in.
You: How many producers and consumers can there be?
Interviewer: Any number of each, all running at the same time.
You: Is it fine if a waiting thread checks the queue every few milliseconds?
Interviewer: No. A waiting thread should sleep and use no CPU until it can make progress.
You: Can I use the language's built-in blocking queue, like Java's ArrayBlockingQueue?
Interviewer: No, that is what we want you to build. You can use locks and condition variables.
You: Do we need a way to close the queue, so consumers stop waiting when production is over?
Interviewer: Not for now. Keep it to enqueue, dequeue and size.
The fourth answer rules out the easiest wrong answer (a loop with sleep). The fifth tells you the level of the tools you are allowed: a lock plus conditions, or Java's synchronized with wait and notify.
💡 Interview tip: Ask whether "full" means wait or fail. A non-blocking
offerthat returnsfalseis a different method with a different test. Many candidates build the wrong one and only notice when the interviewer's example hangs.
Final Requirements
- The queue is created with a fixed
capacityof at least 1. enqueue(x)addsxat the back. If the queue is full, the caller waits until a slot is free.dequeue()removes and returns the front item. If the queue is empty, the caller waits until an item arrives.size()returns how many items are in the queue right now.- Items leave in the order they arrived (FIFO).
- Any number of threads can call any method at the same time. No item is lost or returned twice, and the queue never holds more than
capacityitems. - A waiting thread sleeps: it uses no CPU until it can make progress.
Out of scope
- Timeouts on
enqueueanddequeue(we add them in Extensibility). - Closing the queue and shutting down consumers (also in Extensibility).
- Priorities, persistence, and queues shared between machines.