Vending Machine

A coin-and-note machine modelled as an explicit state machine.

Medium40 minState patternChange makingInventory

Design a vending machine that accepts coins and notes, lets users select a product, dispenses it with change, and supports refunds. It's the textbook State-pattern problem, with a tricky sub-problem hiding inside: making change with limited coins.

Requirements

Functional

  • Products sit in slots (e.g. A1) with price and quantity.
  • Accepts denominations: Rs 1, 2, 5, 10 coins and Rs 20, 50, 100 notes.
  • User inserts money, selects a slot, receives product and change.
  • User can cancel at any time and get a full refund.
  • Reject selection if the product is sold out or funds are insufficient.
  • If exact change can't be made, refuse the sale and refund.
  • Operator mode: restock products, collect cash, refill change.

Non-functional

  • Never lose money: the inserted balance must always be either refunded or converted to a sale plus change.
  • One user at a time (single physical interface), but operator and hardware events must not corrupt state.
  • Easy to add new states (maintenance) and payment types (card, UPI).

Try it first

Set a timer, sketch your own design on paper, then compare it with the reference walkthrough.

40:00

Complete & continue

Defend it out loud

The AI interviewer will probe your design for Vending Machine and score it.

Mock interview