Vending Machine LLD Quiz: State Pattern and Change-Making
Practise vending machine design: ten questions on the State pattern, sold-out handling and change-making, plus a coin box greedy cannot always pay from.
- Course: Low-Level Design study plan
- Module: Vending machine
- Kind: Checkpoint — cleared at 70%
- Reading time: 35 min
- Runtime: OpenJDK 18
Checkpoint — Vending machine is the checkpoint that closes the Vending machine module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the module: the State pattern and its transitions, coins rejected before any state sees them, sold-out slots versus a sold-out machine, restocking, and making change.
How it works. Ten questions and one program. You need 70% on the questions and the program accepted to clear the module.
The program is the hardest in this track so far. The machine now pays change from a coin box holding a limited number of each coin, and inserted coins wait in escrow until a sale. Before you code, decide:
- Which state owns the "can I pay the change?" check, and what happens to the escrow if the answer is no?
- How do you find the fewest coins when greedy can fail? (The amounts are small — a search over counts is fine.)
- What does a cancel return: a greedy refund, or the coins the customer actually inserted?
Hidden cases make change from the customer's own coins, refuse a sale for want of change, and empty the machine.
Common questions
Why does greedy change fail in a real vending machine?
Greedy assumes an unlimited supply of every coin. A real box runs out: paying 6 from one 5 and three 2s, greedy takes the 5 and is stuck, while 2+2+2 works. The machine must search the combinations it can actually pay, or refuse the sale.
What should a vending machine do when it cannot give change?
Refuse the sale before dispensing: return the coins the customer inserted, keep the item, and go back to Idle. Checking the change before the item drops means no customer is ever short-changed.
Exercise
Change from a real coin box
A real machine pays change from the coins it holds. The first line is the number of slots n; each of the next n lines is <code> <name> <price> <stock> (single words, whole numbers). The machine accepts coins of 1, 2, 5 and 10. The next line is the coin box: how many coins of 10, 5, 2 and 1 it holds, in that order. Then events.
Inserted coins are held apart (in escrow) until a sale: CANCEL returns those very coins in the order inserted, joined with +. On a paid selection the inserted coins go into the box, and the change is paid with the fewest coins the box can make it with — among equally few, the one with the most 10s, then 5s, then 2s. If no combination of the box's coins makes the change, the sale is refused: the inserted coins come back out of the box and are returned, the item stays, and the machine is Idle.
| Command | Prints |
|---|---|
INSERT <coin> | rejected <coin> if not 1, 2, 5 or 10; Idle/HasMoney → balance <b>; OutOfStock → sold out, returned <coin> |
SELECT <code> | Idle → insert coins first; OutOfStock → sold out; HasMoney → error: no slot <code>, <code> is sold out, need <k> more, no change for <amount>, returned <coins inserted>, or dispensed <name>, change <coins, largest first> / dispensed <name>, no change |
CANCEL | HasMoney → returned <coins inserted>; otherwise nothing to return |
STATE | Idle, HasMoney or OutOfStock |
COINS | box: 10x<a> 5x<b> 2x<c> 1x<d> |
| anything else | error: unknown command <WORD> |
The machine starts Idle, or OutOfStock if every slot is empty; a sale that empties the last slot moves it to OutOfStock.
In this module: Vending machine
← Vending machine · Elevator system: one car, then a dispatcher →