Problem · Hash Table

Banking Pending Transfer Acceptance

Learn this problem
MediumAnthropicOA

Problem statement

Implement a banking system that reserves outgoing transfers until the recipient accepts them.

Operation Format

Each row in operations contains an operation name followed by string arguments. Timestamps are strictly increasing. Return one row for every operation. Scalar results use one-element rows; TOP_ACTIVITY returns its ranked list directly.

Before processing an operation at timestamp t, expire every unaccepted transfer whose expiration timestamp is strictly less than t and refund its reserved amount to the sender.

Basic Operations

  • ["CREATE_ACCOUNT", timestamp, account_id]: create a unique account with balance zero. Return true or false.
  • ["DEPOSIT", timestamp, account_id, amount]: add funds and return the new balance, or null if the account does not exist.
  • ["PAY", timestamp, account_id, amount]: withdraw funds and return the new balance. Return null for a missing account or insufficient funds.
  • ["TOP_ACTIVITY", timestamp, n]: rank up to n accounts by total processed amount descending, then account ID ascending. Format entries as account_id(total). Deposits, successful payments, and both sides of accepted transfers count; merely creating or expiring a transfer does not.

Pending Transfers

  • ["TRANSFER", timestamp, source_id, target_id, amount]: validate two different existing accounts and sufficient source funds. Reserve the amount immediately and return a globally increasing ID transfer1, transfer2, and so on. Return null on failure.
  • A transfer's expiration timestamp is timestamp + 86400000. It may still be accepted at that exact timestamp; a later operation expires it.
  • ["ACCEPT_TRANSFER", timestamp, account_id, transfer_id]: return true only when account_id is the intended recipient and the transfer is still pending. Credit the recipient, finalize the reserved debit, and add the amount to both accounts' activity totals. Otherwise return false.

Function

bankingPendingTransferAcceptance(operations: String[][]) → String[][]

Examples

Example 1

operations = [["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","bob"],["DEPOSIT","3","alice","500"],["TRANSFER","4","alice","bob","200"],["TOP_ACTIVITY","5","2"],["ACCEPT_TRANSFER","6","bob","transfer1"],["TOP_ACTIVITY","7","2"]]return = [["true"],["true"],["500"],["transfer1"],["alice(500)","bob(0)"],["true"],["alice(700)","bob(200)"]]

The transfer reserves 200 but does not affect activity until Bob accepts it. Acceptance then records 200 for both participants.

Example 2

operations = [["CREATE_ACCOUNT","1","a"],["CREATE_ACCOUNT","2","b"],["DEPOSIT","3","a","100"],["TRANSFER","10","a","b","70"],["ACCEPT_TRANSFER","86400011","b","transfer1"],["PAY","86400012","a","20"]]return = [["true"],["true"],["100"],["transfer1"],["false"],["80"]]

The acceptance occurs one millisecond after the expiration timestamp, so the transfer is refunded before the failed acceptance. Account A can then pay 20 from its restored balance of 100.

More Anthropic problems

drafts saved locally
public String[][] bankingPendingTransferAcceptance(String[][] operations) {
  // write your code here
}
operations[["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","bob"],["DEPOSIT","3","alice","500"],["TRANSFER","4","alice","bob","200"],["TOP_ACTIVITY","5","2"],["ACCEPT_TRANSFER","6","bob","transfer1"],["TOP_ACTIVITY","7","2"]]
expected[["true", "true", "500", "transfer1", "alice(500)", "bob(0)", "true", "alice(700)", "bob(200)"]]
checking account