Design a Vending Machine
🥤 What is a vending machine system? It is the small program inside a snack or drink machine. It takes coins, lets the customer pick a product, drops the product and pays back the change. When the customer changes their mind, it returns their coins. Now and then an operator opens the machine to refill it.
The vending machine is a favorite easy problem because almost every candidate has used one, so the requirements are quick to agree on, and yet it hides two real design questions. The first is that the same button means different things at different times: pressing "select" with no money in does nothing, with money in it sells, and with the door open for service it must be ignored. That is a state machine, and it is the classic place to show the State pattern. The second is change: a machine with a limited number of coins can't always pay it back, and the design has to say what happens then.
A coffee vending machine is the same design with one twist: a drink is made from ingredients instead of taken off a shelf. We build the snack machine first and turn it into a coffee machine in Extensibility.
Requirements
"Design a vending machine. Customers insert coins, pick a product and get their change."
That is usually all you get. The questions below each settle a rule we will code against.
Clarifying Questions
You: What money does it take?
Interviewer: Coins only: 5, 10, 25 and 100 cents. Anything else is rejected and drops back out.
You: Does the machine always have change?
Interviewer: No. It starts with some coins of each kind, and the coins customers pay with go into the same box. If it can't pay the exact change, the sale must not happen.
You: How does it pick the coins for change?
Interviewer: Like a real machine: largest coin first. It can use the coins the customer just put in.
You: What happens when the product is sold out, or the money is not enough?
Interviewer: The selection fails and the money stays in. The customer can add coins, pick something else, or press cancel.
You: What does cancel return?
Interviewer: The same coins the customer inserted.
You: How is the machine refilled?
Interviewer: An operator opens it in service mode. While it is open, nobody can buy anything. The operator can't open it while a customer has money in.
You: Can two customers use it at the same time?
Interviewer: No. It has one coin slot and one keypad.
The answer about change is the one that shapes the design. "The machine always has change" would make change a subtraction. "It may not have change" means we need a coin box that counts coins and a way to plan the change before we promise it.
💡 Interview tip: Ask "what happens when it can't?" for every action: can't make change, can't find the product, can't open the door. Each answer is an error path, and in a vending machine the error paths are most of the design.
Final Requirements
- The machine sells a fixed set of products. Each has a name, a price in cents and a number of items left.
- It accepts coins of 5, 10, 25 and 100 cents. Other coins are rejected.
- A customer inserts coins, then selects a product. The sale succeeds if the product exists, is in stock, the money is enough and the machine can pay the exact change. Then the item drops, the change comes out and the purchase ends.
- Change is paid largest coin first, from the machine's coins plus the coins just inserted.
- If a selection fails, the money stays in. Cancel returns the inserted coins.
- An operator can open service mode only when no purchase is in progress. In service mode products can be restocked, and coins and selections are rejected. Closing service mode makes the machine ready again.
- Anyone can ask how many coins of a value the machine holds (for the operator's report).
Out of scope
- Bills, cards and mobile payments (we discuss cards in Extensibility).
- Coffee recipes and ingredients (Extensibility).
- Hardware: the motor that drops the item, the coin sensor, the display.
- Several customers at once.