DSA Tracker

Low-level design

Design a vending machine

Design a vending machine that takes coins or notes, lets the user pick a product and returns change.

Good first question for: SDE-1

Requirements to confirm first

  • Show products with price and stock, and select one by slot code
  • Accept money in steps and dispense only when the inserted amount covers the price
  • Return change, and refund everything on cancel
  • Refuse a selection when the slot is empty or the change cannot be made
  • Ask: cash only, or cards too? Who refills the machine and collects the cash?

Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.

Show the answer key (write yours first)

Classes and what each one owns

VendingMachine
Holds the current state, the inventory and the cash box, and forwards every button press to the state
MachineState
Interface with one method per user action: insert money, select product, cancel
Idle, HasMoney, Dispensing
The concrete states; each allows only the actions that make sense in it
Inventory
Slot code to product and count
Product
A name and a price
CashBox
Counts of each coin and note, and works out the change

How they connect

  • VendingMachine has exactly one current MachineState and replaces it on a transition
  • States call back into the machine for inventory and cash and hold no data of their own
  • Inventory and CashBox know nothing about states

Design patterns that fit, and why

State
The same button does different things depending on what happened before. Without it every method becomes a chain of status checks.
Strategy
How change is made (largest coin first, or a version that checks which coins are left) can vary.

Code sketch in Java

The core types and one real method. It is a sketch to talk through, not a full program.

interface MachineState {
  void insertMoney(VendingMachine m, int amount);
  void selectProduct(VendingMachine m, String slot);
  void cancel(VendingMachine m);
}

class HasMoney implements MachineState {
  public void selectProduct(VendingMachine m, String slot) {
    Product p = m.inventory().peek(slot);                 // null when sold out
    if (p == null || m.balance() < p.price()) return;
    if (!m.cashBox().canGiveChange(m.balance() - p.price())) return;
    m.setState(new Dispensing(slot));
  }
  // insertMoney adds to the balance; cancel refunds and goes back to Idle
}

Edge cases to raise yourself

  • Largest-coin-first fails for some coin sets even when change exists, and the box can simply be out of a coin
  • The product gets stuck: the money must not be kept, so decide when the sale becomes final
  • Cancel pressed while the machine is dispensing
  • Power is lost between taking the money and dispensing

Where answers usually go wrong

  • One class with a status field and an if-chain in every method. It works for three states and breaks at the first new one, such as a maintenance mode. Name the State pattern and show one transition.
  • Checking stock only at dispense time, after the money has been taken.
  • Assuming change can always be made. Check before accepting the selection, or ask for exact change.

Follow-up questions

  • Add card payment
  • Add a maintenance mode for refilling
  • Report low stock to a server