progress

~/problems / Stateful stores / Bank system

Banking system with scheduled payments

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

medium assessment 4 levels ~90 min Anthropic

Level 1 Accounts, deposits and transfers

Build BankingSystem. Every method takes a timestamp (an integer, in milliseconds) as its first argument. Timestamps strictly increase from one call to the next, across all methods. Account ids are non-empty strings, and amounts are positive integers.

  • create_account(timestamp, account_id) -> bool: open an account with balance 0. Return False if an account with that id already exists.
  • deposit(timestamp, account_id, amount) -> int | None: add amount to the account and return its new balance. Return None if the account doesn't exist.
  • transfer(timestamp, source_id, target_id, amount) -> int | None: move amount from the source account to the target account and return the source's new balance. Return None, changing nothing, if either account doesn't exist, the two ids are the same, or the source's balance is less than amount.
bank = BankingSystem()
bank.create_account(1, "a")           # True
bank.create_account(2, "b")           # True
bank.create_account(3, "a")           # False: "a" exists
bank.deposit(4, "a", 500)             # 500
bank.deposit(5, "zz", 10)             # None: no such account
bank.transfer(6, "a", "b", 200)       # 300 (b now has 200)
bank.transfer(7, "b", "a", 201)       # None: b has only 200
bank.transfer(8, "a", "a", 1)         # None: same account

Constraints: up to 10^5 calls, amounts up to 10^9. Balances can pass 2^31, so use 64-bit integers in C++ and Java.

Later levels build on this code: payments that run later, merges, and balances at past times. Each level unlocks when the previous one passes.

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.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc