DesignTheSystem/dev
EASY Low-Level Design (LLD) 12 automated tests

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.

DifficultyEASY
Design focusStates and the actions each state allows
Automated grading3 public + 9 hidden tests (12 total)
Phase 2 follow-upMaintenance mode
LanguageTypeScript (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 (core)

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 (core)

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 (edge)

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

More low-level design problems