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 INPUT Initial Balance Array Acc 1 10 Acc 2 100 Acc 3 20 Acc 4 50 Acc 5 30 Operations: 1. withdraw(3, 10) 2. transfer(5, 1, 20) 3. deposit(5, 20) 4. transfer(3, 4, 15) 5. withdraw(10, 50) Bank System ALGORITHM STEPS 1 Validate Account Check if acc in [1, n] 2 Check Balance balance[acc] >= amount 3 Execute Operation Update balance array 4 Return Result true/false status Op Check Result withdraw(3,10) 20>=10 OK transfer(5,1,20) 30>=20 OK deposit(5,20) valid acc OK transfer(3,4,15) 10<15 FAIL withdraw(10,50) acc>n FAIL FINAL RESULT Final Balance State Acc 1 30 Acc 2 100 Acc 3 10 Acc 4 50 Acc 5 30 Output Array: [true,true,true,false,false] Transaction Results: withdraw(3,10): OK - 20-10=10 transfer(5,1,20): OK deposit(5,20): OK - 10+20=30 transfer(3,4,15): FAIL bal withdraw(10,50): FAIL acc Key Insight: Optimized Validation: Each operation requires O(1) time - validate account bounds first, then check sufficient balance for withdraw/transfer. Invalid accounts (like acc 10) fail immediately. Insufficient funds (acc3 has 10, needs 15) also return false without mutation. TutorialsPoint - Simple Bank System | Optimized Validation Approach
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