Design an ATM
🏧 What is an ATM? It is the cash machine in the wall of a bank. You insert your card, type your PIN, and then you can check your balance, take out cash or pay notes in. The machine asks the bank about your account, counts out notes from its own cassettes, and gives the card back when you are done.
The ATM is a favorite because it mixes three things in one small problem. There is a session with clear steps (no cash before a PIN), which is a natural state machine. There is money in two places, the account at the bank and the notes in the machine, and the two must never disagree. And there is a shared account that two machines can touch at the same moment, which brings in concurrency. A good answer keeps each of these in its own class.
Requirements
"Design an ATM. Customers insert a card, enter a PIN and can check their balance, withdraw and deposit cash."
Most interviewers stop there. The questions below are the ones that change the classes.
Clarifying Questions
You: Is the bank part of the design, or a system we call?
Interviewer: A separate system in real life. Model it as a simple class behind the ATM so we can run it: accounts with a card number, a PIN and a balance.
You: What happens after wrong PINs?
Interviewer: Three wrong PINs in a row block the card. A correct PIN resets the count.
You: Does the count reset when the card comes out?
Interviewer: No. It belongs to the card, not to the visit. Otherwise someone could guess three PINs, take the card out, and start again.
You: Which notes does the machine hold, and how does it choose them?
Interviewer: 10s, 20s, 50s and 100s. Pay the largest notes first. If that can't make the exact amount, refuse the withdrawal.
You: If a withdrawal fails halfway, what state are we left in?
Interviewer: It can't fail halfway. Either the customer gets all the money and the account is charged, or nothing happens at all.
You: Can one account be used from two machines at the same time?
Interviewer: Yes. A joint account has two cards, and two people can be at two machines at once. The balance must never go below zero.
You: Are there daily limits or fees?
Interviewer: Not for now. Maybe later.
The third answer is a small security lesson and the reason the counter lives at the bank, not in the machine. The fifth answer is the one that shapes the withdrawal code: "all or nothing" means we must work out the whole payout before anything changes.
💡 Interview tip: When money is involved, always ask "what if this fails halfway?". Interviewers rarely say it first, and the answer (plan everything, then commit) is what separates a working ATM from one that loses cash.
Final Requirements
- The bank holds accounts. Each account has a card number, a PIN and a balance.
- One customer uses the machine at a time. A session starts when a known, unblocked card is inserted and ends when the card is ejected.
- Balance checks, withdrawals and deposits are only allowed after the correct PIN in the current session. After that, any number of them is allowed.
- Three wrong PINs in a row block the card for good and end the session. The count is per card, survives ejecting the card, and resets after a correct PIN.
- A withdrawal pays with the largest notes first. It is refused if the account balance is too low or the machine cannot make the exact amount that way.
- A withdrawal is all or nothing: the account is charged and notes leave the machine together, or neither happens.
- A deposit adds the notes to the machine and their value to the balance.
- Withdrawals from the same account on different machines at the same time must never take the balance below zero.
Out of scope
- Transfers, bill payments, statements and PIN changes (we come back to some of these in Extensibility).
- Fees, daily limits and foreign cards.
- Networking, encryption and the physical card reader, keypad and cassettes.
- Unblocking a card (the bank's support desk does that).