Banking system with transfers, cashback and account merges
I was asked this
No reports yet. Sign in to say where you were asked it.
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. ReturnsFalseif the id already exists. -
deposit(timestamp, account_id, amount) -> int | None: adds money and returns the new balance. ReturnsNoneif the account does not exist. -
transfer(timestamp, source_id, target_id, amount) -> int | None: moves money and returns the source's new balance. ReturnsNoneif 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 tonaccounts 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 with0.
Level 3: payments with delayed cashback
-
pay(timestamp, account_id, amount) -> str | None: withdrawsamountand returns a payment id"payment1","payment2", ..., numbered globally across all accounts. ReturnsNoneif 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 timetis applied before any other operation at timet. -
get_payment_status(timestamp, account_id, payment) -> str | None: returns"IN_PROGRESS"or"CASHBACK_RECEIVED". ReturnsNoneif 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. ReturnsFalseif 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 attime_at(time_at <= timestamp). ReturnsNoneif 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.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.


