Design the Sleeping Barber
💈 What is the sleeping barber problem? A barbershop has one barber, one barber chair and a few waiting chairs. When nobody is waiting, the barber falls asleep in his chair. A customer who walks in wakes him up if he is asleep, sits in a waiting chair if he is busy, or leaves if every chair is taken. The barber and every customer are separate threads, and we write the shop they share.
The story is old, but the shape is everywhere: a worker that should sleep when there is no work, a waiting area with a fixed size, callers that are turned away when it is full, and callers that wait for their own result. That is a thread pool with a bounded queue and a reject policy, a print server, or a support desk. Interviewers use the barber because it hides two bugs that are easy to write and hard to see: a wake-up that gets lost, and a customer who is told "done" before their haircut is.
Requirements
"Model a barbershop with one barber and N waiting chairs. The barber sleeps when there are no customers. Customers wake him, wait if he is busy, and leave if the waiting room is full."
Most of the design hides in what the prompt does not say. Ask first.
Clarifying Questions
You: One barber, or several?
Interviewer: One for now.
You: When every waiting chair is taken, does the customer wait outside, or leave?
Interviewer: They leave right away. The shop does not block them.
You: When the barber is free, which waiting customer goes next?
Interviewer: The one who has waited longest.
You: Does a customer's call return when they sit down, or when their haircut is finished?
Interviewer: When the haircut is finished. A customer must not walk out with half a haircut.
You: Does the barber chair count as one of the N chairs?
Interviewer: No. N waiting chairs, plus the barber chair.
You: How long does a haircut take? Should the shop time it?
Interviewer: That is the barber's business. He takes a customer, cuts, and then says he is done.
You: Are the barber and the customers threads we write, or do they call into us?
Interviewer: They are threads that call into your shop object. You write the shop.
The fourth answer is the one people miss. It turns the problem from "a queue with a sleeping consumer" into "a queue plus a reply for each customer": the customer needs to know when their haircut is done, not just that some haircut finished.
💡 Interview tip: For every blocking call in a concurrency problem, ask exactly when it should return. "When I sit down" and "when my haircut is done" lead to different designs, and the second one needs a signal per caller.
Final Requirements
- The shop has one barber, one barber chair and
Nwaiting chairs, fixed when it opens. - A customer who arrives when every waiting chair is taken leaves at once (the call returns
false). - Otherwise the customer sits in a waiting chair. If the barber is asleep, the arrival wakes him.
- The barber takes the customer who has waited longest; that customer's waiting chair becomes free. If nobody is waiting, the barber sleeps until someone arrives. He never spins checking.
- A customer's call returns
trueonly after the barber has finished that customer's haircut. - No lost wake-ups: a customer must never sit in the waiting room while the barber sleeps.
- Anyone can ask how many customers are waiting.
Out of scope
- Several barbers, closing time, and customers who give up (all in Extensibility).
- How long a haircut takes, prices and payment.
- Appointments and walk-in priorities.