AlgoViz

Design a cash machine (ATM)

hard 85 min

Card, PIN, notes.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 5:10

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

AshacustomerM1CashMachinephaseREADYbankBankfree300held0boxCashBoxtotal140journalJournalM1-1nonerollersDispenserseennothingwithdraw(60)

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

M1CashMachinephaseREADYbankBankfree220held20journalJournalM1-3PAYINGrollersDispenserseenunknowntaken?unknown

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_LATER

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Take the money, then pay

    critical

    A jam or a power cut in between charges people for nothing.

    Wrong
    account.balance -= amountdispenser.dispense(amount)
    Right
    bank.hold(ref, card, amount)dispenser.pay(ref, notes)bank.commit(ref)
  • A timeout taken for a no

    critical

    The hold may have arrived. Nobody will release it.

    Wrong
    except Timeout:    return REFUSED
    Right
    except BankSilent:    self._finish(ref)      # release, now or later
  • One error for every failure

    critical

    Unsure is refunded like an empty feed: free money.

    Wrong
    except DispenserError:    bank.release(ref)
    Right
    except NothingCameOut:    note(ref, RETURNED)except DispenserUnsure:    pass                   # ask the sensors
  • Greedy notes

    major

    80 from one 50 and four 20s is refused.

    Wrong
    n = min(left // v, notes[v])left -= n * v
    Right
    for n in range(min(left // v, notes[v]), -1, -1):    ...                    # and go back
  • Balance in the machine

    major

    Two machines, two copies, one account.

    Wrong
    if self.accounts[card].balance >= amount:
    Right
    if 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.