Practice/Anthropic/Banking System (Online Assessment)
CodingMust
You are implementing an in-memory banking system with four progressive levels of complexity. Each level builds on the previous one, adding new features and operations.
This is a typical online assessment (OA) problem where levels are unlocked sequentially. Plan your design carefully to accommodate future requirements.
Initially, the banking system contains no accounts. Implement operations for account creation, deposits, and transfers.
create_account(timestamp, account_id)
True if successful, False if account already existsdeposit(timestamp, account_id, amount)
None if account doesn't existtransfer(timestamp, source_account_id, target_account_id, amount)
Transfers money from source to target account
Returns source account balance after transfer
Returns None if:
bank = BankingSystem() bank.create_account(1, "acc1") # True bank.deposit(2, "acc1", 100) # 100 bank.create_account(3, "acc2") # True bank.transfer(4, "acc1", "acc2", 30) # 70 (acc1 balance)
The bank wants to identify accounts with high outgoing transactions. Implement ranking by total spending.
top_spenders(timestamp, n)
n accounts by total outgoing transactions["account_id_1(total_1)", "account_id_2(total_2)", ...]` bank.top_spenders(100, 3)
`
Support withdrawals with cashback rewards that are credited after a delay.
pay(timestamp, account_id, amount)
None if account doesn't exist or has insufficient fundsget_payment_status(timestamp, account_id, payment)
"IN_PROGRESS" if cashback not yet received"CASHBACK_RECEIVED" if cashback has been creditedNone if invalid account/payment` payment_id = bank.pay(1000, "acc1", 100)
status = bank.get_payment_status(5000, "acc1", payment_id)
status = bank.get_payment_status(90000000, "acc1", payment_id)
`
int(amount * 0.02)timestamp + 86400000top_spenders to include pay operationsSupport merging accounts while preserving history, and querying past balances.
merge_accounts(timestamp, account_id_1, account_id_2)
account_id_2 into account_id_1account_id_2 after mergeTrue if successful, False if invalidget_balance(timestamp, account_id, time_at)
time_atNone if account didn't exist at that timeaccount_id_2 added to account_id_1account_id_2 redirected to account_id_1account_id_1)top_spendersaccount_id_2 can be recreated after merge (starts fresh)` bank.merge_accounts(1000, "acc1", "acc2")
balance = bank.get_balance(2000, "acc1", 500)
`
time_at < merge_time return pre-merge balanceaccount_id_2 can be created again with fresh state