Problem · Design

Banking System, Part 1: Accounts and Transfers

Learn this problem
EasyAnthropicOA

Problem statement

Banking System series

Implement the first level of a simplified banking system. The system starts with no accounts and processes operations in strictly increasing timestamp order.

Operation Format

Each row in operations contains an operation name followed by its arguments, all represented as strings. Return one row for every operation:

  • A scalar result is returned as a one-element row, such as ["true"] or ["750"].
  • A source-level None result is returned as ["null"].

Level 1 Operations

  • ["CREATE_ACCOUNT", timestamp, account_id]: Create an account with balance 0 if the ID does not exist. Return true on success and false if the account already exists.
  • ["DEPOSIT", timestamp, account_id, amount]: Add amount to the account. Return its new balance, or null if the account does not exist.
  • ["TRANSFER", timestamp, source_account_id, target_account_id, amount]: Transfer money between two different accounts. Return the source account's new balance on success. Return null if either account does not exist, both IDs are equal, or the source has insufficient funds.

Function

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

Examples

Example 1

operations = [["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","bob"],["DEPOSIT","3","alice","1000"],["TRANSFER","4","alice","bob","250"],["DEPOSIT","5","bob","50"]]return = [["true"],["true"],["1000"],["750"],["300"]]

Alice receives 1000, sends 250 to Bob, and keeps 750. Bob then has 250 from the transfer plus the 50 deposit.

Example 2

operations = [["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","alice"],["DEPOSIT","3","missing","100"],["TRANSFER","4","alice","alice","10"],["CREATE_ACCOUNT","5","bob"],["TRANSFER","6","alice","bob","10"],["DEPOSIT","7","alice","100"],["TRANSFER","8","alice","bob","40"]]return = [["true"],["false"],["null"],["null"],["true"],["null"],["100"],["60"]]

The duplicate creation, missing-account deposit, same-account transfer, and insufficient-funds transfer fail. After Alice receives 100, the final transfer succeeds and leaves her with 60.

Constraints

  • 1 <= timestamp <= 10^9
  • All timestamps are unique and operations are provided in strictly increasing timestamp order.
  • Amounts are positive integers and all numeric results fit in a signed 64-bit integer.
  • Every operation has exactly the arguments shown above.

More Anthropic problems

drafts saved locally
public String[][] bankingSystemLevel1(String[][] operations) {
  // write your code here
}
operations[["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","bob"],["DEPOSIT","3","alice","1000"],["TRANSFER","4","alice","bob","250"],["DEPOSIT","5","bob","50"]]
expected[["true", "true", "1000", "750", "300"]]
checking account