Design a cash machine (ATM)
hard 85 minCard, PIN, notes.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
A machine between a bank and a dispenser: watch the journal line before each step.
Operations
A good withdrawal
Asha takes 60 from 300. The box holds 140.
Step 1 of 8
The wish
The PIN was right, so the phase is READY.
Step 1: The wish
def withdraw(self, amount): self._need(Phase.READY) notes = self._box.plan(amount) if notes is None: return Result.NO_SUCH_NOTES ref = f"{self._name}-{next(self._numbers)}" self._journal.write(Entry(ref, Step.ASKED, self._card, amount, notes)) try: approved = self._bank.hold(ref, self._card, amount) except BankSilent: self._finish(ref) return Result.TRY_LATER if not approved: self._note(ref, Step.DONE) return Result.REFUSED self._note(ref, Step.PAYING) try: self._dispenser.pay(ref, notes) self._note(ref, Step.PAID) self._box.take(notes) except NothingCameOut: self._note(ref, Step.RETURNED) except DispenserUnsure: pass return self._finish(ref)- Cost
- Three bank calls at most
- Guarantee
- Four journal lines
- Take, then pay
- charged for jams
Look at the journal: it is always one step ahead of the world.
In the real world
The same cash machine (atm) doing real jobs. Pick one and step through it.
The sensors do not know. A person decides.
Step 1 of 4
Unsure
The shutter opened and the rollers stopped.
Step 1: Unsure
def _finish(self, ref): if self._journal.last(ref).step is Step.PAYING: taken = self._dispenser.taken(ref) if taken is None: self._note(ref, Step.SUSPECT) self.phase = Phase.OUT_OF_SERVICE return Result.CALL_STAFF self._note(ref, Step.PAID if taken else Step.RETURNED) if taken: self._box.take(self._journal.last(ref).notes) paid = self._journal.last(ref).step is Step.PAID try: if paid: self._bank.commit(ref) else: self._bank.release(ref) self._note(ref, Step.DONE) except BankSilent: pass return Result.PAID if paid else Result.TRY_LATERCommon mistakes
The bugs interviewers watch for, with the fix beside each.
Take the money, then pay
criticalA jam or a power cut in between charges people for nothing.
Wrongaccount.balance -= amountdispenser.dispense(amount)Rightbank.hold(ref, card, amount)dispenser.pay(ref, notes)bank.commit(ref)A timeout taken for a no
criticalThe hold may have arrived. Nobody will release it.
Wrongexcept Timeout: return REFUSEDRightexcept BankSilent: self._finish(ref) # release, now or laterOne error for every failure
criticalUnsure is refunded like an empty feed: free money.
Wrongexcept DispenserError: bank.release(ref)Rightexcept NothingCameOut: note(ref, RETURNED)except DispenserUnsure: pass # ask the sensorsGreedy notes
major80 from one 50 and four 20s is refused.
Wrongn = min(left // v, notes[v])left -= n * vRightfor n in range(min(left // v, notes[v]), -1, -1): ... # and go backBalance in the machine
majorTwo machines, two copies, one account.
Wrongif self.accounts[card].balance >= amount:Rightif self._bank.hold(ref, card, amount):
Interview tips
Open with
- Is the bank another system?
- Charged exactly when notes left.
- Hold, pay, commit.
Reject aloud
- Account is the bank's.
- A PIN is an argument.
- A transaction tree is a journal.
Prove it
- Stop after each line.
- Release before hold.
- charged = notes outside.
Never
- A silent no.
- A void dispense.
- A refund after unsure.