Design the Unisex Bathroom
🚻 What is the unisex bathroom problem? An office has one bathroom that men and women share. Men and women may not be inside at the same time, and the room only fits a few people. Every person is a thread that wants to go in, stay a moment and come out. We write the object that decides who may go in and who has to wait.
This is a classic synchronization puzzle, and a cousin of the readers-writers problem: instead of "many readers or one writer", it is "many of one group or many of the other". Interviewers like it because the rules fit on a sticky note, yet the first working answer almost always has a hidden flaw: one group can keep the other out forever. Spotting that flaw, and fixing it without breaking anything else, is what the round is really about.
Requirements
"Design a bathroom shared by men and women. It can never have both inside at once, and it holds at most N people. Many people use it at the same time."
The prompt sounds complete, but it leaves out every decision that shapes the code. Ask before you write anything.
Clarifying Questions
You: What does a "person" look like in code? Is each one a thread?
Interviewer: Yes. Each person is a thread that calls enter, stays a moment, then calls leave. Many threads call in at once.
You: When someone cannot go in yet, should enter return false, or wait?
Interviewer: Wait. enter returns only once the person is inside.
You: Is the capacity fixed, and the same for both groups?
Interviewer: Fixed when the bathroom is built, and the same for everyone.
You: Suppose men keep arriving while a few men are inside. Can a woman who is waiting be kept out for as long as men keep coming?
Interviewer: No. Nobody should wait forever. That is the part I care most about.
You: Does the bathroom have to let people in strictly in arrival order, or just make sure nobody waits forever?
Interviewer: Nobody waiting forever is the requirement. If you pick a stricter policy, tell me what it costs.
You: Can someone call leave without having entered, or leave twice?
Interviewer: No, assume callers are well behaved.
You: Do people ever give up and walk away from the line?
Interviewer: Not for now. Maybe later.
The fourth question is the one that matters. Without it you will write a correct-looking answer that lets one group starve the other: a thread that is ready to go is kept waiting indefinitely, not because of a deadlock, but because others keep jumping ahead of it.
💡 Interview tip: In any problem with "groups that share a resource" (readers and writers, men and women, two directions of traffic on a one-lane bridge), ask about starvation before you code. It is the requirement interviewers most often hold back to see if you think of it.
Final Requirements
- The bathroom has a fixed capacity
N, set when it is created. enter(gender)blocks until the person may go in, then returns with them inside.leave(gender)lets them out and never waits for long.- Men and women are never inside at the same time.
- Never more than
Npeople are inside. - No deadlock: if the room is empty and someone is waiting, someone gets in.
- No starvation: a person who is waiting gets in after a bounded number of others, however many new people arrive.
- Any number of threads may call
enterandleaveat the same time.
Out of scope
- People who give up waiting, and time limits (we come back to this in Extensibility).
- More than two groups, such as cleaning staff (also in Extensibility).
- Individual stalls, queues per door, or anything about the physical room.
- Several bathrooms or several buildings: that is a distributed problem, not a single object.