cswork / OA打开练习工作台

ZipRecruiterOA #1

Banking System (4 Progressive Levels)

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

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

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

Implement a simplified version of a banking system. All operations have a timestamp parameter (stringified ms, range 1 to 10⁹, strictly increasing).

Level 1 — Account Creation, Deposit, Transfer

  • boolean createAccount(int timestamp, String accountId) — creates a new account. Returns true if successful, false if account already exists.
  • Optional<Integer> deposit(int timestamp, String accountId, int amount) — deposits given amount to specified account. Returns the balance after the operation. Returns Optional.empty() if account doesn't exist.
  • Optional<Integer> transfer(int timestamp, String sourceAccountId, String targetAccountId, int amount) — transfer amount from source to target. Returns balance of source after the transfer, or Optional.empty() if either account doesn't exist, source == target, or source has insufficient funds.

Level 2 — Top Spenders by Outgoing Transactions

  • List<String> topSpenders(int timestamp, int n) — return the top n accounts ordered by highest total money withdrawn from the account (outgoing transactions only — transfer source and pay count). If two have the same total, order lexicographically by account id. Format: "accountId(amount)".

Level 3 — Scheduled Payments with Cashback

  • Optional<String> pay(int timestamp, String accountId, int amount) — withdraw given amount. All withdraw transactions provide a 2% cashback — 2% of the withdrawn amount (rounded down) will be refunded to the account 24 hours after the withdrawal. If the withdrawal is successful (i.e., the account has sufficient funds to perform the given amount), returns a unique payment identifier "payment{ordinal}" (e.g. "payment1", "payment2", etc.). Additional conditions:

  • Returns Optional.empty() if the account doesn't exist.

  • Returns Optional.empty() if the account has insufficient funds.

  • topSpenders should now also account for total amount of money withdrawn from accounts via pay.

  • The waiting period for cashback is 24 hours, equal to 24 * 60 * 60 * 1000 = 86,400,000 ms.

  • When it's time to process cashback for a withdrawal, the amount must be refunded to the account before any other transactions are performed at the relevant timestamp.

  • Optional<String> getPaymentStatus(int timestamp, String accountId, String paymentId) — return the payment status. Returns Optional.empty() if the account doesn't exist, or the payment doesn't exist for the specified account. Otherwise return string representing the payment status: "IN_PROGRESS" or "CASHBACK_RECEIVED".

Level 4 — Account Merging with History

  • boolean mergeAccounts(int timestamp, String accountId1, String accountId2) — merge accountId2 into accountId1. Returns false if accountId1 == accountId2 or any account doesn't exist. Returns true if accounts were successfully merged. Specifically:

  • All pending cashback refunds for accountId2 should still be processed, but refunded to accountId1 instead.

  • After the merge, it must be possible to check the status of payment identifiers belonging to accountId2 by replacing accountId2 with accountId1.

  • The balance of accountId2 should be added to the balance for accountId1.

  • topSpenders should recognize merged accounts — total outgoing transactions for merged accounts should be the sum of all money transferred and/or withdrawn in both accounts.

  • accountId2 should be removed from the system after the merge.

  • Optional<Integer> getBalance(int timestamp, String accountId, int timeAt) — return the total amount of money in the account at the given timestamp timeAt. If the specified account did not exist at a given time timeAt, returns Optional.empty().

  • If queries have been processed at timestamp timeAt, getBalance must reflect the account balance after the query has been processed.

  • If the account was merged into another account, the merged account should inherit its balance history.