Design Dining Philosophers
🍝 What is the dining philosophers problem? Five philosophers sit around a round table with a bowl of spaghetti each. There is one fork between every pair of neighbors, so five forks in total. A philosopher needs both the fork on the left and the fork on the right to eat. The puzzle is to write the rules each philosopher follows so that nobody gets stuck and nobody goes hungry forever.
It is the oldest teaching example for deadlock, and interviewers still use it because it is tiny and every wrong answer fails in a different, instructive way. The obvious answer deadlocks. The first fix often serializes the whole table. The second fix avoids deadlock but can still let one philosopher starve. A strong answer walks through all three and ends with a design you can defend line by line. LeetCode 1226 asks for exactly this with an interface built from callbacks, and that is the interface we use here.
Requirements
"Five philosophers share five forks around a round table. Each one needs two forks to eat. Make sure they can all keep eating."
The prompt sounds like a riddle, but every clarifying question below settles a rule in the code.
Clarifying Questions
You: What exactly is a philosopher in code?
Interviewer: A thread. Each philosopher's thread calls wantsToEat once per meal and passes in five actions: pick up the left fork, pick up the right fork, eat, put down the left fork, put down the right fork. Your class decides when each action may run.
You: Is the table always five seats?
Interviewer: Make it n philosophers, at least two. Philosopher p uses fork p on the left and fork (p + 1) % n on the right.
You: When one of the pick-up actions runs, does that mean the fork is really in the philosopher's hand?
Interviewer: Yes. Two neighbors must never hold the same fork at the same time. Only call the action once the fork is yours.
You: Is it fine if only one philosopher eats at a time, as long as it is safe?
Interviewer: No. Two philosophers who do not share a fork should be able to eat at the same time. Don't lock the whole table.
You: Can a philosopher be skipped forever while their neighbors keep eating?
Interviewer: No. Everyone who is hungry must eventually eat. I care about that as much as about deadlock.
You: Can I add a coordinator, or must each philosopher decide alone?
Interviewer: The DiningPhilosophers object is yours. Put any shared state in it you like.
The fifth answer is the one candidates forget to ask. "No deadlock" and "no starvation" sound alike, but they are different properties and the usual textbook fix only gives you the first one.
💡 Interview tip: Say the three properties out loud before you write code: safety (no shared fork), liveness (no deadlock: someone always makes progress) and fairness (no starvation: everyone eventually makes progress). Then check each design against all three. It turns a riddle into a checklist.
Final Requirements
- There are
n >= 2philosophers, numbered0ton - 1around the table. Philosopherp's left fork is forkp, and the right fork is fork(p + 1) % n. - Each philosopher calls
wantsToEat(p, pickLeft, pickRight, eat, putLeft, putRight)from their own thread, once per meal. Each call runs every action exactly once: both pick-ups (in either order), theneat, then both put-downs. - Safety: a fork is held by at most one philosopher at a time, and
eatruns only while the philosopher holds both forks. - No deadlock: every call to
wantsToEateventually returns. - No starvation: a hungry philosopher eats after a bounded number of neighbor meals, however the threads are scheduled.
- Concurrency: philosophers who are not neighbors can eat at the same time.
Out of scope
- How long philosophers think or eat. The callbacks take as long as they take.
- Giving up after a timeout, and philosophers joining or leaving mid-dinner (both in Extensibility).
- Several tables on several machines. This is one process with shared memory.