~/problems / Stateful stores / Bank system

OA: Bank system

medium 4 levels ~90 min Anthropic

Level 1 Accounts and money

Build Bank. Every method takes a timestamp (an integer, milliseconds) first. Timestamps strictly increase from one call to the next. Amounts are positive integers.

  • create_account(timestamp, account_id) -> bool: open an account with balance 0. False if an account with that id already exists.
  • deposit(timestamp, account_id, amount) -> int | None: add money and return the new balance. None if the account doesn't exist.
  • withdraw(timestamp, account_id, amount) -> int | None: take money out and return the new balance. None (changing nothing) if the account doesn't exist or has less than amount.
  • transfer(timestamp, source_id, target_id, amount) -> int | None: move money right away and return the source's new balance. None (changing nothing) if either account doesn't exist, they're the same account, or the source has less than amount.
bank = Bank()
bank.create_account(1, "a"); bank.create_account(2, "b")
bank.deposit(3, "a", 500)          # 500
bank.withdraw(4, "a", 100)         # 400
bank.transfer(5, "a", "b", 150)    # 250
bank.transfer(6, "b", "a", 999)    # None

Level 2 unlocks when level 1 passes.

Level 3 unlocks when level 2 passes.

Level 4 unlocks when level 3 passes.

Topic: Bank system. Accounts, transfers that must be accepted, merges; state machines.

0:00
Ctrl ' run · Ctrl ↵ submit
esc