FastPrepProcess Banking Operations

Process Banking Operations

Vercel logoVercel● MediumFULLTIMEOA
Learn

Problem statement

Maintain account balances while processing a finite ordered list of banking operations. Each operation is a string array.

  • ["CREATE", account] creates an account with balance zero and returns 1, or returns 0 if it already exists.
  • ["DEPOSIT", account, amount] adds a positive amount and returns the new balance, or -1 for an unknown account.
  • ["TRANSFER", from, to, amount] moves a positive amount between distinct accounts and returns the source account's new balance, or -1 if either account is unknown, the accounts are equal, or the source lacks funds.
  • ["BALANCE", account] returns the current balance, or -1 for an unknown account.

Return one integer result per operation in input order. A rejected operation does not change any balance.

Function

processBankingOperations(operations: String[][]) → long[]

Examples

Example 1

operations = [["CREATE","alice"],["DEPOSIT","alice","20"],["BALANCE","alice"]]return = [1,20,20]

The operations create one account, deposit funds, and read its balance in order.

Example 2

operations = [["CREATE","a"],["CREATE","b"],["DEPOSIT","a","9"],["TRANSFER","a","b","4"],["TRANSFER","a","b","8"],["BALANCE","b"]]return = [1,1,9,5,-1,4]

The successful transfer leaves five in a and four in b; the next transfer is rejected without mutation.

Constraints

  • 1 <= operations.length <= 100000
  • Account names are nonempty strings of letters and digits with length at most 40.
  • Transaction amounts are decimal integers between 1 and 1000000000.
  • Every balance fits signed 64-bit arithmetic.
  • Each operation has exactly the fields described above.

More Vercel problems

See Vercel hiring insights
public long[] processBankingOperations(String[][] operations) {
    // Write your code here.
}
operations[["CREATE","alice"],["DEPOSIT","alice","20"],["BALANCE","alice"]]
expected[1,20,20]
Checking account…