Design Vending Machine (Low-Level Design)
Take coins, sell products, give change, and refuse what a state does not allow. Focus: States and the actions each state allows.
| Difficulty | EASY |
|---|---|
| Design focus | States and the actions each state allows |
| Automated grading | 3 public + 9 hidden tests (12 total) |
| Phase 2 follow-up | Maintenance mode |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
The machine accepts coins of 5, 10, 25 and 100 cents. Anything else is refused with an error.
Selecting a product sells it if it is in stock and enough money was inserted. Change is returned as coins, largest first, using the fewest coins (the machine has unlimited coins of each kind).
Cancel returns everything inserted, as coins, largest first, using the fewest coins.
Functional requirements
- insertCoin(cents) adds to the balance, or throws for an unknown coin.
- select(code) returns { product, change } and resets the balance; it throws (and keeps the balance) for an unknown code, no stock or too little money.
- cancel() returns the balance as coins and resets it.
- balance() and stock(code) report the current state.
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
interface ProductSpec {
code: string;
name: string;
price: number; // cents
stock: number;
}
class VendingMachine {
constructor(products: ProductSpec[]) {}
insertCoin(cents: number): void {
throw new Error("Not implemented");
}
balance(): number {
throw new Error("Not implemented");
}
select(code: string): { product: string; change: number[] } {
throw new Error("Not implemented");
}
/** Returns the inserted money as coins, largest first */
cancel(): number[] {
throw new Error("Not implemented");
}
stock(code: string): number {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 9 hidden edge-case tests in a sandbox:
Buy with exact money
var m = new VendingMachine([{ code: "A1", name: "Water", price: 100, stock: 2 }]);
m.insertCoin(100);
assertEqual(m.select("A1"), { product: "Water", change: [] });
assertEqual(m.stock("A1"), 1);
Change is given in the fewest coins
var m = new VendingMachine([{ code: "B2", name: "Chips", price: 65, stock: 1 }]);
m.insertCoin(100);
assertEqual(m.select("B2").change, [25, 10]);
Unknown coins are refused
var m = new VendingMachine([]);
assertThrows(function () { m.insertCoin(3); });
assertEqual(m.balance(), 0);
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- The machine's modes are modelled as states (state classes or a named state type): Each mode allows different actions. Modelling states makes the allowed actions obvious and keeps checks from being scattered across every method.
- Making change is its own piece (a class or function), not repeated: Change is computed for sales, cancels and maintenance refunds. One place means one place to fix.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 12 tests, you unlock the change request: Maintenance mode.
Relevant low-level design lessons
State: what each mode allows
Model modes so each action asks the current state whether it is allowed.
Strategy: swap behaviour, not code
Replace an if/else on a type with interchangeable strategy classes.
Polymorphism at scale
Give each kind of thing its own class when the behaviour really differs.
More low-level design problems
Design Tic-Tac-Toe (N × N)
Any board size, with a win check that only looks around the last move.
Design Snake and Ladder
A board game where the board, the players and the rules for a turn are separate pieces.
Design Shopping Cart
Already solved, with comments: add items, apply tier discounts, then coupons. Run it and submit it to see how grading works.
Design Parking Lot
Park vehicles in the right size of spot, issue tickets and charge on exit.