Simple Bank System - Problem

You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has n accounts numbered from 1 to n. The initial balance of each account is stored in a 0-indexed integer array balance, with the (i + 1)th account having an initial balance of balance[i].

Execute all the valid transactions. A transaction is valid if:

  • The given account number(s) are between 1 and n, and
  • The amount of money withdrawn or transferred from is less than or equal to the balance of the account.

Implement the Bank class:

  • Bank(long[] balance) Initializes the object with the 0-indexed integer array balance.
  • boolean transfer(int account1, int account2, long money) Transfers money dollars from the account numbered account1 to the account numbered account2. Return true if the transaction was successful, false otherwise.
  • boolean deposit(int account, long money) Deposit money dollars into the account numbered account. Return true if the transaction was successful, false otherwise.
  • boolean withdraw(int account, long money) Withdraw money dollars from the account numbered account. Return true if the transaction was successful, false otherwise.

Input & Output

Example 1 — Basic Operations
$ Input: balance = [10,100,20,50,30], operations = [["withdraw",3,10],["transfer",5,1,20],["deposit",5,20],["transfer",3,4,15],["withdraw",10,50]]
Output: [true,true,true,false,false]
💡 Note: withdraw(3,10): Account 3 has 20, withdraw 10 → balance becomes 10, return true. transfer(5,1,20): Account 5 has 30, transfer 20 to account 1 → account 5: 10, account 1: 30, return true. deposit(5,20): Deposit 20 to account 5 → account 5: 30, return true. transfer(3,4,15): Account 3 has 10, insufficient balance for 15, return false. withdraw(10,50): Account 10 doesn't exist, return false.
Example 2 — Invalid Operations
$ Input: balance = [100], operations = [["withdraw",1,200],["transfer",1,2,50]]
Output: [false,false]
💡 Note: withdraw(1,200): Account 1 has 100, insufficient balance for 200, return false. transfer(1,2,50): Account 2 doesn't exist (only 1 account), return false.
Example 3 — Deposit Only
$ Input: balance = [0,0,0], operations = [["deposit",1,100],["deposit",2,50],["deposit",3,25]]
Output: [true,true,true]
💡 Note: All deposits to valid accounts succeed: account 1: 100, account 2: 50, account 3: 25.

Constraints

  • 1 ≤ balance.length ≤ 105
  • 0 ≤ balance[i] ≤ 1012
  • 1 ≤ account, account1, account2 ≤ n
  • 1 ≤ money ≤ 1012
  • At most 104 calls in total

Visualization

Tap to expand
Simple Bank System OverviewInitial Balances1010020503012345Operationswithdraw(3, 10) → truetransfer(5, 1, 20) → truedeposit(5, 20) → truetransfer(3, 4, 15) → falsewithdraw(10, 50) → falseFinal Result[true, true, true, false, false]
Understanding the Visualization
1
Input
Initial account balances and list of operations
2
Process
Validate and execute each banking operation
3
Output
Boolean array indicating success/failure of each operation
Key Takeaway
🎯 Key Insight: Account validation and balance checking are crucial for secure banking operations
Asked in
Amazon 15 Microsoft 12 Google 8 Apple 6
32.0K Views
Medium Frequency
~15 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen