MetaOA #11
Banking System (4-Level Design)
估计困难评测数据准备中,可先阅读题面
Implement a simplified banking system. The full problem comes in four progressive levels — each level adds new operations on top of the previous one. All operations carry a timestamp parameter (milliseconds, strictly ascending across calls, range 1 to 10⁹).
Level 1 — accounts, deposits, transfers
boolean createAccount(int timestamp, String accountId)— create the account if it does not already exist. Returnstrueon success,falseifaccountIdalready exists.Optional<Integer> deposit(int timestamp, String accountId, int amount)— creditamounttoaccountId. Returns the new balance, orOptional.empty()if the account does not exist.Optional<Integer> transfer(int timestamp, String sourceAccountId, String targetAccountId, int amount)— moveamountfrom source to target. Returns the source balance after the transfer, orOptional.empty()if either account is missing, the two ids are equal, or the source has insufficient funds.
Level 2 — outgoing ranking
List<String> topSpenders(int timestamp, int n)— return the topnaccounts by total outgoing money (transfers out pluspay-withdrawals introduced in Level 3; cashback refunds do NOT count). Sort by outgoing descending, ties broken byaccountIdascending. Format each entry"<accountId>(<totalOutgoing>)". If fewer thannaccounts exist, return all of them.
Level 3 — scheduled payments with 2% cashback
Optional<String> pay(int timestamp, String accountId, int amount)— withdrawamount. On success return"payment<k>"wherekis the 1-indexed global withdrawal counter. The withdrawal triggers a cashback offloor(amount * 0.02)that is refunded to the same account at exactlytimestamp + 86_400_000(24 hours later). At any timestamp, due cashback refunds must be applied BEFORE the operation at that timestamp runs. ReturnsOptional.empty()if the account is missing or has insufficient funds. Withdrawals count towardtopSpenderstotals; cashback refunds do not.Optional<String> getPaymentStatus(int timestamp, String accountId, String payment)— return"IN_PROGRESS"or"CASHBACK_RECEIVED". ReturnsOptional.empty()if the account does not exist, the payment id does not exist, or the payment belongs to a different account.
Level 4 — merging accounts and historical balance
boolean mergeAccounts(int timestamp, String accountId1, String accountId2)— mergeaccountId2intoaccountId1. After the mergeaccountId2is removed.accountId1inherits the balance, the outgoing total (fortopSpenders), the pending cashbacks (refunded toaccountId1when due), and the payment-status lookup foraccountId2's payment ids. Returnsfalseif the two ids are equal or either is missing.Optional<Integer> getBalance(int timestamp, String accountId, int timeAt)— return the balance ofaccountIdat the moment immediately after the operation attimeAtfinished. If the account did not exist attimeAt(either not yet created or already merged away), returnOptional.empty(). A merged-away account's history transfers to the surviving account.
Execution time limit 20 s, memory limit 4 GB.