← Questions

Banking system with transfers, cashback and account merges

0
AnthropicAirbnbMetaCodingLast asked Sep 2026

Build an in-memory banking system in four levels. Each level adds methods, and every earlier method must keep working. Every call takes a timestamp in milliseconds as its first argument. Timestamps strictly increase across calls. Amounts are positive integers. A failed call returns None.

Level 1: accounts and transfers

  • create_account(timestamp, account_id) -> bool: creates an account with balance 0. Returns False if the id already exists.
  • deposit(timestamp, account_id, amount) -> int | None: adds money and returns the new balance. Returns None if the account does not exist.
  • transfer(timestamp, source_id, target_id, amount) -> int | None: moves money and returns the source's new balance. Returns None if either account is missing, if they are the same account, or if the source does not have enough money.

Level 2: ranking

  • top_spenders(timestamp, n) -> list[str]: returns up to n accounts ranked by total outgoing money (successful transfers out, plus payments from Level 3), highest first, with ties broken by account id ascending. Format each entry as "id(total)". Accounts with no outgoing money are included with 0.

Level 3: payments with delayed cashback

  • pay(timestamp, account_id, amount) -> str | None: withdraws amount and returns a payment id "payment1", "payment2", ..., numbered globally across all accounts. Returns None if the account is missing or does not have enough money. Exactly 24 hours later (timestamp + 86_400_000), 2% of the amount, rounded down, is credited back to the account. Cashback due at time t is applied before any other operation at time t.
  • get_payment_status(timestamp, account_id, payment) -> str | None: returns "IN_PROGRESS" or "CASHBACK_RECEIVED". Returns None if the account does not exist or the payment does not belong to it.

Level 4: merges and history

  • merge_accounts(timestamp, account_id_1, account_id_2) -> bool: merges account 2 into account 1. Account 1 receives account 2's balance and outgoing total, plus its payments and any cashback still pending. Account 2 is then deleted. Returns False if the ids are equal or either account is missing.
  • get_balance(timestamp, account_id, time_at) -> int | None: returns the account's balance as it was at time_at (time_at <= timestamp). Returns None if the account did not exist at that time. A merged-away account still reports its balance for times before the merge.

Example

create_account(1, "kim")                -> True
create_account(2, "lee")                -> True
deposit(3, "kim", 1000)                 -> 1000
transfer(4, "kim", "lee", 300)          -> 700
pay(5, "lee", 150)                      -> "payment1"   (lee: 150)
top_spenders(6, 2)                      -> ["kim(300)", "lee(150)"]
get_payment_status(7, "lee", "payment1")-> "IN_PROGRESS"
merge_accounts(8, "kim", "lee")         -> True        (kim: 850)
deposit(86_400_005, "kim", 10)          -> 863         (cashback 3 landed first)
get_payment_status(86_400_006, "kim", "payment1") -> "CASHBACK_RECEIVED"
get_balance(86_400_007, "lee", 6)       -> 150
get_balance(86_400_007, "lee", 9)       -> None

Constraints

  • Up to 10^5 calls in total. Balances fit in 64-bit integers.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding