Vending Machine

State Machine Design

Design a machine that takes coins, sells a product, returns change and lets the customer cancel. The interviewer is looking for the State pattern done properly, and for what happens when the machine cannot make change.

Overview

A customer puts coins in, picks a product by its code, and gets the product and any change back. They can cancel and get their coins back at any point before the product drops. The machine keeps stock and a box of coins, refuses what it cannot do, and is never left half-way through a sale.

This is the standard exercise for the State pattern. The machine behaves differently depending on where it is in a sale: a coin is welcome while paying and refused while a product is dropping; a cancel refunds coins in one state and means nothing in another. Interviewers watch whether you put each of those rules where it belongs, in a state class, instead of in a growing if-chain, and whether you notice the one rule most candidates miss: a sale must be checked for change before the product is released.

Scoping the problem

Six questions set the boundaries of the machine. Each comes with why it matters and the answer interviewers usually give.

  • Which coins, and are bills or cards in scope? Payment is a strategy, and starting with coins keeps the state machine in focus. The usual answer is coins only, with cards as a follow-up.
  • Can a product be selected before paying? This decides whether there is a Selected state before Paying. The usual answer is pay first, then select.
  • What if the machine cannot make exact change? It can refuse the sale or keep the difference. The usual answer is to refuse, keep the customer's coins and let them cancel.
  • Can the customer cancel, and when? Cancel is valid while paying, not while dispensing. The usual answer is yes, until the product is released.
  • One customer at a time? Yes. The machine is a single shared resource, so one lock goes around each action.
  • Who restocks and empties the cash box? An operator API outside the sale flow does. The usual answer is a restock method, with a maintenance state as a follow-up.

Functional requirements

  • Accept coins of fixed denominations (5, 10, 25 and 100 cents) one at a time
  • Select a product by code once enough money is in; the machine checks stock, funds and change before it commits
  • Dispense the product and the exact change, made from coins the machine holds
  • Cancel at any point while paying and get every inserted coin back
  • Refuse actions that make no sense in the current state, with a clear error and no side effect
  • Restock products and load coins through operator methods

Non-functional requirements

  • Every action is atomic: one lock on the machine, so a kiosk API cannot interleave two customers
  • A refused sale leaves the machine exactly as it was: stock, cash box and the customer's coins unchanged
  • The three state objects hold no per-sale data, so they are created once and reused
  • Adding a state (for example Maintenance) or a payment method is a new class, not a change to the machine

Interview questions

  • Why the State pattern instead of a status field and if statements?
  • How would you add card payments?
  • Should the state objects be created once or on every transition?
  • What happens when the machine cannot make change?
  • How do you keep a sale from being left half done?
  • How would you add a Maintenance state for restocking?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio