System design · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix

Vending Machine

mediumlldstate-machine

Prompt

"Design a vending machine. Customers put in money, pick a product, get the product and change. Show me how you model the machine's states and what happens on each input."

Clarifying questions

  • What payment forms? Coins and notes in fixed denominations. Card payment is a follow-up.
  • Can a customer insert money and then cancel? Yes; the machine refunds the inserted amount.
  • What if the exact change cannot be made? The machine should refuse the sale before dispensing and refund. Assume the machine's coin inventory is tracked.
  • What if a product is out of stock? Refuse the selection, stay in the "has money" state so the customer can pick something else or cancel.
  • Multiple customers at once? No; a physical machine serves one customer at a time. But an operator restocks and an API reads inventory, so some concurrency exists.
  • Does the machine need to persist state? Money and inventory should survive a power cycle. Mention it, model it as a hook.

Requirements

Functional

  • Accept money in valid denominations and show the running balance.
  • Select a product by slot code; dispense it when the balance covers the price and change can be made.
  • Return change in coins from the machine's own inventory.
  • Cancel at any point before dispensing, refunding the balance.
  • Operator functions: restock a slot, load coins, collect cash.

Non-functional

  • Illegal sequences (select before paying, insert during dispensing) must be rejected, not silently ignored or half-applied.
  • Every transition must leave the machine in a consistent state even if dispensing fails mid-way.
  • Adding a state (out of service, maintenance) or a payment method must not require editing every existing operation.

Numbers

  • Perhaps 30 slots, a coin inventory of a few hundred coins across 5 denominations, one transaction every few minutes. Performance is irrelevant; this problem is graded entirely on correctness of transitions and on structure.

Last reviewed 8 September 2026