cswork / OA打开练习工作台

MetaOA #11

Banking System (4-Level Design)

估计困难评测数据准备中,可先阅读题面

考点(AI 标注)迭代器设计哈希计数模拟/实现

同类可判练习:银行账本(四关递进)

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. Returns true on success, false if accountId already exists.
  • Optional<Integer> deposit(int timestamp, String accountId, int amount) — credit amount to accountId. Returns the new balance, or Optional.empty() if the account does not exist.
  • Optional<Integer> transfer(int timestamp, String sourceAccountId, String targetAccountId, int amount) — move amount from source to target. Returns the source balance after the transfer, or Optional.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 top n accounts by total outgoing money (transfers out plus pay-withdrawals introduced in Level 3; cashback refunds do NOT count). Sort by outgoing descending, ties broken by accountId ascending. Format each entry "<accountId>(<totalOutgoing>)". If fewer than n accounts exist, return all of them.

Level 3 — scheduled payments with 2% cashback

  • Optional<String> pay(int timestamp, String accountId, int amount) — withdraw amount. On success return "payment<k>" where k is the 1-indexed global withdrawal counter. The withdrawal triggers a cashback of floor(amount * 0.02) that is refunded to the same account at exactly timestamp + 86_400_000 (24 hours later). At any timestamp, due cashback refunds must be applied BEFORE the operation at that timestamp runs. Returns Optional.empty() if the account is missing or has insufficient funds. Withdrawals count toward topSpenders totals; cashback refunds do not.
  • Optional<String> getPaymentStatus(int timestamp, String accountId, String payment) — return "IN_PROGRESS" or "CASHBACK_RECEIVED". Returns Optional.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) — merge accountId2 into accountId1. After the merge accountId2 is removed. accountId1 inherits the balance, the outgoing total (for topSpenders), the pending cashbacks (refunded to accountId1 when due), and the payment-status lookup for accountId2's payment ids. Returns false if the two ids are equal or either is missing.
  • Optional<Integer> getBalance(int timestamp, String accountId, int timeAt) — return the balance of accountId at the moment immediately after the operation at timeAt finished. If the account did not exist at timeAt (either not yet created or already merged away), return Optional.empty(). A merged-away account's history transfers to the surviving account.

Execution time limit 20 s, memory limit 4 GB.