Restaurant Management
Tables, orders, kitchen stations and the bill
Design the system behind a sit-down restaurant: seat a party, take an order, send each item to the right kitchen station, tell the server when the food is ready, and produce a bill that can be split.
Overview
A restaurant is a small factory with a dining room attached. Guests arrive and need a table. A server takes an order; each line on it goes to a different part of the kitchen, the burger to the grill, the fries to the fryer, the drinks to the bar. Cooks finish items at different times, someone has to notice when a whole order is ready, and at the end the guests want one bill, or three.
The interview version asks you to model that flow with a handful of classes and to get four things right. A table must move through its life in order: free, occupied, being cleaned, free again. An item must go to the station that makes it, and must stop being cancelable once a cook has started on it. Money must be in whole cents and a split bill must add up to the total, to the cent. And the host stand must not seat two parties at one table when two hosts click at the same moment.
A strong answer draws the two life cycles first (a table's and an item's), shows that routing is a lookup on the menu item rather than an if-else, and puts money in integer cents with a rule for the leftover penny. The code below does all of that.
Scoping the problem
Before drawing anything, ask these questions, because each answer adds or removes a class. Each bullet gives the question, why it matters, and the answer used in the rest of this chapter.
- Walk-ins only, or reservations too? Reservations add a time window to a table's state. This design handles walk-ins; reservations are a follow-up.
- Can an order change after it is sent to the kitchen? That decides whether items have a status of their own. Yes, until a cook starts the item.
- One kitchen, or stations? Stations mean routing and per-station queues. This design uses stations, one queue each.
- Who is told when food is ready? A server needs a notification per item or per order. Both: item listeners, plus an all-ready check per order.
- How is the bill split? It can be even, by item or custom. A strategy handles it: an even split in the code, by item in a follow-up question.
- What about tax and tip? Rates vary by place and must not be hard-coded. The tax rate is a constructor parameter in basis points, and the tip is a number.
- Many terminals at once? Then the seating step and the station queues need locks. Yes, there are many.
Functional requirements
- seat(party_size) gives the party the smallest free table with enough seats, or None
- place_order(table, lines) creates or extends the table's order and sends each item to the station named on its menu item
- cancel_item(item) is allowed only while the item is PENDING; a cooking or ready item cannot be canceled
- A station pulls the next pending item with start_next and reports it with mark_ready; ready listeners are called
- order.all_ready() tells the expo when every live item of an order is ready or served
- bill(table, tip) computes subtotal, tax at the configured rate and total in cents; canceled items cost nothing
- bill.split(strategy) returns per-guest shares that sum exactly to the total
- close_table moves the table to CLEANING and table_cleaned moves it back to FREE; illegal moves raise
Non-functional requirements
- No table is seated twice: seating is atomic under one lock
- Station queues are safe with several servers adding and cooks pulling at once
- Money is integer cents; the tax rate is a parameter, never a literal in the code
- New stations, menu items and split rules are added as data or one class, with no change to place_order or Bill
- Every state change of a table or item is checked against an allowed-moves rule
Interview questions
- How does an item reach the right station, and how does the server know the whole order is ready?
- A guest changes their mind after the order is sent. What can still change?
- How do you split a bill by item instead of evenly, and keep the cents right?
- Two hosts on two terminals seat parties at the same time. How do you keep them from taking the same table?
- How would you add reservations?
- What has to survive a crash, and what can be rebuilt?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio