Design an ATM System as a State Machine with Greedy Cash Dispensing
Quick Overview
Design an ATM as a finite state machine with idle, card inserted and authenticated states, a bank that checks PINs and holds balances, and a cash dispenser that pays withdrawals greedily from five note denominations. It tests state guards, class responsibilities and keeping balances and note counts consistent when a withdrawal fails.
Design an ATM System as a State Machine with Greedy Cash Dispensing
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an ATM modeled as a finite state machine with three states: `IDLE`, `CARD_INSERTED` and `AUTHENTICATED`. The ATM coordinates three collaborators: the user (who provides the card and PIN), the bank (which validates credentials and adjusts account balances) and the cash dispenser (which holds banknotes in fixed denominations and pays out cash).
Starting in `IDLE`, the ATM moves to `CARD_INSERTED` when a card is inserted, and to `AUTHENTICATED` after a successful PIN check. From `AUTHENTICATED` the user may check the balance, deposit or withdraw cash. Ejecting the card returns the ATM to `IDLE`.
The cash dispenser stocks five denominations: 500, 200, 100, 50 and 20. Withdrawals are dispensed with a greedy strategy: go from the largest denomination to the smallest and take as many notes of each as the remaining amount and the stock allow. A withdrawal succeeds only if this pass assembles the amount exactly and the amount does not exceed the account balance. Every operation except the two setup methods returns a status string describing the outcome.
Implement the following methods:
- `registerAccount(cardNumber: str, pin: int, balance: int)` registers a bank account with a card number, PIN and initial balance.
- `loadCash(denomination: int, count: int)` loads `count` notes of the given `denomination` into the dispenser.
- `insertCard(cardNumber: str) -> str` inserts a card and moves `IDLE` to `CARD_INSERTED`. Returns the new state name or an error string.
- `enterPin(pin: int) -> str` submits a PIN. On success it moves `CARD_INSERTED` to `AUTHENTICATED`. Returns the new state name or an error string.
- `checkBalance() -> str` returns the balance of the authenticated account as `BALANCE:<amount>`, or an error string.
- `deposit(amount: int) -> str` deposits `amount` into the authenticated account. Returns `SUCCESS` or an error string.
- `withdraw(amount: int) -> str` withdraws `amount` using the greedy denomination selection. Returns `SUCCESS` or an error string.
- `ejectCard() -> str` ejects the card and returns to `IDLE`. Returns `EJECTED` or an error string.
- `getState() -> str` returns the current state name: `IDLE`, `CARD_INSERTED` or `AUTHENTICATED`.
Each method runs its checks in the order below and returns the error for the first one that fails. A call that returns an error changes nothing.
| Method | Checks, in order |
|---|---|
| `insertCard` | not in `IDLE`: `ERROR:INVALID_STATE`; unknown card: `ERROR:CARD_NOT_FOUND` |
| `enterPin` | not in `CARD_INSERTED`: `ERROR:INVALID_STATE`; wrong PIN: `ERROR:WRONG_PIN` (the card stays in) |
| `checkBalance`, `deposit`, `withdraw` | not in `AUTHENTICATED`: `ERROR:NOT_AUTHENTICATED` |
| `deposit` | amount not positive: `ERROR:INVALID_AMOUNT` |
| `withdraw` | amount not positive or not a multiple of 10: `ERROR:INVALID_AMOUNT`; balance below the amount: `ERROR:INSUFFICIENT_FUNDS`; greedy pass leaves a remainder: `ERROR:INSUFFICIENT_CASH` |
| `ejectCard` | already in `IDLE`: `ERROR:INVALID_STATE` |
Example 1, a full session:
```text
registerAccount("4111", 1234, 1000)
loadCash(500, 1)
loadCash(100, 2)
loadCash(20, 5)
getState() -> "IDLE"
checkBalance() -> "ERROR:NOT_AUTHENTICATED"
insertCard("9999") -> "ERROR:CARD_NOT_FOUND"
insertCard("4111") -> "CARD_INSERTED"
enterPin(1111) -> "ERROR:WRONG_PIN"
enterPin(1234) -> "AUTHENTICATED"
withdraw(760) -> "SUCCESS"
checkBalance() -> "BALANCE:240"
withdraw(100) -> "ERROR:INSUFFICIENT_CASH"
withdraw(300) -> "ERROR:INSUFFICIENT_FUNDS"
withdraw(25) -> "ERROR:INVALID_AMOUNT"
deposit(60) -> "SUCCESS"
checkBalance() -> "BALANCE:300"
ejectCard() -> "EJECTED"
ejectCard() -> "ERROR:INVALID_STATE"
```
The withdrawal of 760 takes one 500, two 100s and three 20s, which leaves two 20s in the machine. The next withdrawal of 100 fails on cash: no 100s or 50s are left, and two 20s make only 40. The withdrawal of 300 reports funds, because the balance is 240 and the balance check runs before the cash check.
Example 2, greedy refuses an amount another mix could pay:
```text
registerAccount("5500", 4321, 500)
loadCash(50, 2)
loadCash(20, 3)
insertCard("5500") -> "CARD_INSERTED"
withdraw(100) -> "ERROR:NOT_AUTHENTICATED"
enterPin(4321) -> "AUTHENTICATED"
insertCard("5500") -> "ERROR:INVALID_STATE"
withdraw(60) -> "ERROR:INSUFFICIENT_CASH"
withdraw(140) -> "SUCCESS"
checkBalance() -> "BALANCE:360"
ejectCard() -> "EJECTED"
getState() -> "IDLE"
```
For 60 the greedy pass takes one 50 and is left with 10, which no note can pay, so the withdrawal is refused even though three 20s would make 60. For 140 it takes two 50s and two 20s.
```hint Guard every call
Start each public method by asking whether the call is legal in the current state. Most of the error strings come from that one check.
```
```hint Two systems, one outcome
A withdrawal changes both the account and the note counts. Work out everything that can still fail before you change either one.
```
### Constraints & Assumptions
- `1 <= pin <= 10^9` and `1 <= amount <= 10^9`.
- Supported banknote denominations: 20, 50, 100, 200, 500. `loadCash` ignores any other denomination.
- A withdrawal amount must be a multiple of 10.
- At most `10^4` operations are invoked.
- Calling `registerAccount` with an existing card number overwrites that account.
- A deposit credits the account balance only. It does not add notes to the dispenser.
### Clarifying Questions to Ask
- Is there a limit on wrong PIN attempts, and should the machine keep the card when it is reached?
- Is the bank an in-process component that always answers, or a remote service that can time out?
- Can the same account be used from two ATMs at the same time?
- Will the machine need more states later, such as out of service or maintenance?
### What a Strong Answer Covers
- The state transitions, and how each method rejects a call that is not legal in the current state.
- How responsibilities split between the bank, the cash dispenser and the ATM controller, and which class owns which data.
- How `withdraw` keeps the account balance and the note counts consistent when a later check fails.
- Greedy dispensing with limited note stock, including the amounts it refuses.
- Edge cases (unknown card, wrong PIN, unsupported denomination, eject while idle) and the cost per operation.
### Follow-up Questions
- The greedy pass refuses 60 when the machine holds 50s and three 20s. How would you pay every amount the stock can cover, and what does that cost?
- How would you add a three-attempt PIN limit that keeps the card?
- If the bank is a remote service that can time out after it debits the account, how do you keep the balance and the cash in step?
- How would you add an `OUT_OF_SERVICE` state without editing every method?
Overview: Design an ATM as a finite state machine with idle, card inserted and authenticated states, a bank that checks PINs and holds balances, and a cash dispenser that pays withdrawals greedily from five note denominations. It tests state guards, class responsibilities and keeping balances and note counts consistent when a withdrawal fails.
Design an ATM modeled as a finite state machine with three states: IDLE, CARD_INSERTED and AUTHENTICATED. The ATM coordinates three collaborators: the user (who provides the card and PIN), the bank (which validates credentials and adjusts account balances) and the cash dispenser (which holds banknotes in fixed denominations and pays out cash).
Starting in IDLE, the ATM moves to CARD_INSERTED when a card is inserted, and to AUTHENTICATED after a successful PIN check. From AUTHENTICATED the user may check the balance, deposit or withdraw cash. Ejecting the card returns the ATM to IDLE.
The cash dispenser stocks five denominations: 500, 200, 100, 50 and 20. Withdrawals are dispensed with a greedy strategy: go from the largest denomination to the smallest and take as many notes of each as the remaining amount and the stock allow. A withdrawal succeeds only if this pass assembles the amount exactly and the amount does not exceed the account balance. Every operation except the two setup methods returns a status string describing the outcome.
Implement the following methods:
registerAccount(cardNumber: str, pin: int, balance: int)
registers a bank account with a card number, PIN and initial balance.
loadCash(denomination: int, count: int)
loads
count
notes of the given
denomination
into the dispenser.
insertCard(cardNumber: str) -> str
inserts a card and moves
IDLE
to
CARD_INSERTED
. Returns the new state name or an error string.
enterPin(pin: int) -> str
submits a PIN. On success it moves
CARD_INSERTED
to
AUTHENTICATED
. Returns the new state name or an error string.
checkBalance() -> str
returns the balance of the authenticated account as
BALANCE:<amount>
, or an error string.
deposit(amount: int) -> str
deposits
amount
into the authenticated account. Returns
SUCCESS
or an error string.
withdraw(amount: int) -> str
withdraws
amount
using the greedy denomination selection. Returns
SUCCESS
or an error string.
ejectCard() -> str
ejects the card and returns to
IDLE
. Returns
EJECTED
or an error string.
getState() -> str
returns the current state name:
IDLE
,
CARD_INSERTED
or
AUTHENTICATED
.
Each method runs its checks in the order below and returns the error for the first one that fails. A call that returns an error changes nothing.
Method
Checks, in order
insertCard
not in IDLE: ERROR:INVALID_STATE; unknown card: ERROR:CARD_NOT_FOUND
enterPin
not in CARD_INSERTED: ERROR:INVALID_STATE; wrong PIN: ERROR:WRONG_PIN (the card stays in)
checkBalance, deposit, withdraw
not in AUTHENTICATED: ERROR:NOT_AUTHENTICATED
deposit
amount not positive: ERROR:INVALID_AMOUNT
withdraw
amount not positive or not a multiple of 10: ERROR:INVALID_AMOUNT; balance below the amount: ERROR:INSUFFICIENT_FUNDS; greedy pass leaves a remainder: ERROR:INSUFFICIENT_CASH
The withdrawal of 760 takes one 500, two 100s and three 20s, which leaves two 20s in the machine. The next withdrawal of 100 fails on cash: no 100s or 50s are left, and two 20s make only 40. The withdrawal of 300 reports funds, because the balance is 240 and the balance check runs before the cash check.
Example 2, greedy refuses an amount another mix could pay:
For 60 the greedy pass takes one 50 and is left with 10, which no note can pay, so the withdrawal is refused even though three 20s would make 60. For 140 it takes two 50s and two 20s.
Constraints & Assumptions
1 <= pin <= 10^9
and
1 <= amount <= 10^9
.
Supported banknote denominations: 20, 50, 100, 200, 500.
loadCash
ignores any other denomination.
A withdrawal amount must be a multiple of 10.
At most
10^4
operations are invoked.
Calling
registerAccount
with an existing card number overwrites that account.
A deposit credits the account balance only. It does not add notes to the dispenser.
Clarifying Questions to Ask Guidance
Is there a limit on wrong PIN attempts, and should the machine keep the card when it is reached?
Is the bank an in-process component that always answers, or a remote service that can time out?
Can the same account be used from two ATMs at the same time?
Will the machine need more states later, such as out of service or maintenance?
What a Strong Answer Covers Guidance
The state transitions, and how each method rejects a call that is not legal in the current state.
How responsibilities split between the bank, the cash dispenser and the ATM controller, and which class owns which data.
How
withdraw
keeps the account balance and the note counts consistent when a later check fails.
Greedy dispensing with limited note stock, including the amounts it refuses.
Edge cases (unknown card, wrong PIN, unsupported denomination, eject while idle) and the cost per operation.
Follow-up Questions Guidance
The greedy pass refuses 60 when the machine holds 50s and three 20s. How would you pay every amount the stock can cover, and what does that cost?
How would you add a three-attempt PIN limit that keeps the card?
If the bank is a remote service that can time out after it debits the account, how do you keep the balance and the cash in step?
How would you add an
OUT_OF_SERVICE
state without editing every method?