ATM System
Banking Machine
Design a cash machine: card and PIN, a state machine for the session, a chain of note handlers that plans before it dispenses, and money that never goes wrong.
Overview
A cash machine serves one customer at a time. The customer inserts a card, enters a PIN, and then asks for a balance or a withdrawal. The machine talks to the bank to check the PIN and to move money, and it holds a stock of notes in a few values. When the customer takes the card, the machine is ready for the next person.
The problem is a medium one because each part is simple and the difficulty is in the order of operations. What the keypad may do depends on where the session is, which is a state machine. A withdrawal must take the money from the account and hand out the notes, and either both happen or neither. The notes have to be assembled from the values on hand, which is a small planning problem with a trap in it: taking the largest note first does not always work.
Interviewers use this problem to see whether you keep the session states as classes rather than a pile of flags, whether you plan a withdrawal before you change anything, and whether you handle money as integers. A float balance is the most common mistake in this problem, and it is a real bug, not a style point.
Scoping the problem
Ask these questions before you write code. Each answer settles a piece of the design, and the usual answers are the ones built here.
- Which operations: balance, withdrawal, deposit, transfer? Each is a state or a branch in the authenticated state. Usually balance and withdrawal, with deposit as a follow-up.
- Who checks the PIN, the machine or the bank? This decides what the machine may store. The bank does; the machine stores nothing about the PIN.
- How many PIN attempts, and what happens after? It is a counter in one state, and the card is blocked and kept. Usually three, then block and retain.
- Which note values does the machine hold? This decides the dispensing chain and its failure cases. Usually 100, 50 and 20.
- What if the account has the money but the machine cannot make the amount? The account must not be debited. Plan first, then debit, then dispense.
- Can the same account be used from two machines at once? Then the account needs a lock, and the machine needs one too. Usually yes.
- Receipts, a printer, a camera, a cash deposit slot? Hardware is out of scope; a transaction record is in, kept as a list of transactions.
Functional requirements
- Insert a card; a blocked card is refused
- Enter a PIN, checked by the bank against a hash; three wrong attempts block the card and the machine keeps it
- Show the balance and withdraw an amount once authenticated
- A withdrawal is refused when the balance is too low or the notes on hand cannot make the amount, and then nothing changes
- Notes are handed out largest value first from the machine's stock of 100, 50 and 20
- Eject the card and return to idle; every completed withdrawal is recorded as a transaction
Non-functional requirements
- Money is integer cents; no float ever holds a balance or an amount
- A withdrawal either debits the account and dispenses, or does neither
- Two machines using one account at the same time cannot overdraw it
- The machine never stores a PIN in the clear; the comparison is constant-time
- Adding an operation or a state touches one class each
Interview questions
- The greedy chain refuses 180 even though 100 plus four 20s works. How do you fix it?
- Why integer cents and not a float balance?
- What if the hardware jams after the account was debited?
- How would you add deposits and transfers?
- Where is the concurrency in a machine that serves one customer at a time?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio