Design an ATM (Low-Level Design)
Design an ATM as a State machine (Idle → HasCard → Authenticated → Dispensing), model transactions as polymorphic Command classes, and dispense cash with a Chain of Responsibility over denominations — with the correctness details interviewers probe.
🟠Low-Level Design · Case Study · Senior
"Design an ATM" is a rich LLD problem because it's a clear state machine (insert card → authenticate → choose transaction → dispense) with a couple of nice sub-problems: modelling different transaction types cleanly, and the cash-dispensing algorithm. It combines several patterns naturally. Let's design it.
Step 1 — Requirements
- Functional: insert card, authenticate (PIN), choose a transaction — withdraw / deposit / balance check, dispense cash, print/eject; talk to the bank for balance.
- Non-functional: enforce the right flow (can't withdraw before authenticating), never over-dispense, handle insufficient funds / insufficient cash in the machine.
Step 2 — The classes
- ATM — the context; holds the current state, the cash dispenser, and the current session.
- ATMState (interface) → IdleState, HasCardState, AuthenticatedState, DispensingState.
- Card, Account (via a BankService), Transaction → Withdraw / Deposit / BalanceInquiry.
- CashDispenser — holds counts of each note denomination and dispenses.
Step 3 — The state machine
🟠Like the vending machine, the ATM's rules depend entirely on state, so State is the backbone — invalid actions (withdraw before PIN) simply don't exist in the wrong state.
Step 4 — Transactions & cash dispensing
🔴 Two clean pattern choices:
- Transactions = polymorphism (Command/Strategy): a
Transactioninterface withexecute(); Withdraw, Deposit, BalanceInquiry each implement it. Adding a new transaction type is a new class — and you can log/audit them uniformly (that's the Command benefit). - Cash dispensing = Chain of Responsibility: to dispense $180 from $100/$50/$20 notes, chain handlers — the $100 handler dispenses what it can and passes the remainder to the $50 handler, and so on. Clean, extensible, and a great place to name the pattern.
abstract class CashHandler { // Chain of Responsibility
protected CashHandler next; protected int denom; protected int count;
void dispense(int amount) {
int num = Math.min(amount / denom, count);
if (num > 0) { count -= num; /* release num × denom notes */ }
int remaining = amount - num * denom;
if (remaining > 0 && next != null) next.dispense(remaining);
else if (remaining > 0) throw new InsufficientCashException();
}
}
Step 5 — The senior nuances
🔴 Mention: validate sufficient account balance AND sufficient machine cash before dispensing (fail cleanly if either is short — and don't debit the account if you can't dispense); handle PIN retry limits (lock after 3 failures); make the withdraw atomic so a debit without a dispense can't happen (or reverse it if dispense fails). These correctness details matter for anything touching money.
The interview-ready walkthrough
"I'd model the ATM as a State machine — Idle → HasCard → Authenticated → Dispensing — so invalid actions are impossible by construction. Transactions (withdraw/deposit/balance) are polymorphic classes behind a Transaction interface (Command-style, so they're auditable). Cash dispensing uses a Chain of Responsibility over note denominations, passing the remainder down the chain. Critically, I check both account balance and machine cash before dispensing and make the withdraw atomic so I never debit without dispensing, plus PIN retry limits." That's a pattern-rich, correctness-aware answer."
What to read next
- State Pattern · Chain of Responsibility — the two patterns here
- Design a Vending Machine — the sibling state machine
- Design a Logging Framework — next up
- ← The complete System Design guide (hub)