Simple Bank System - Problem

You're building the core system for a modern digital bank that needs to handle millions of transactions daily. The bank manages n accounts numbered from 1 to n, where each account has an initial balance stored in a 0-indexed array.

Your mission: Implement a robust Bank class that can safely execute three types of financial operations:

  • ๐Ÿ’ธ Transfer: Move money between two accounts
  • ๐Ÿ’ฐ Deposit: Add money to an account
  • ๐Ÿง Withdraw: Remove money from an account

Security Rules: A transaction is valid only if:

  1. All account numbers are between 1 and n
  2. The account has sufficient balance for withdrawals/transfers

Your system should return true for successful transactions and false for invalid ones, ensuring no money is lost or created!

Input & Output

example_1.py โ€” Basic Operations
$ Input: Bank([10, 100, 20, 50, 30]) operations: deposit(5, 20), withdraw(3, 10), transfer(1, 2, 5)
โ€บ Output: deposit: true, withdraw: true, transfer: true
๐Ÿ’ก Note: All operations are valid: account 5 gets $20 (30โ†’50), account 3 loses $10 (20โ†’10), and $5 moves from account 1 to 2 (10โ†’5, 100โ†’105)
example_2.py โ€” Invalid Operations
$ Input: Bank([10, 100, 20, 50, 30]) operations: withdraw(6, 10), transfer(1, 6, 5), withdraw(1, 50)
โ€บ Output: withdraw: false, transfer: false, withdraw: false
๐Ÿ’ก Note: Account 6 doesn't exist (invalid), transfer to invalid account fails, and account 1 has insufficient balance ($10 < $50)
example_3.py โ€” Edge Cases
$ Input: Bank([100]) operations: withdraw(1, 100), deposit(1, 50), transfer(1, 1, 25)
โ€บ Output: withdraw: true, deposit: true, transfer: true
๐Ÿ’ก Note: Single account bank: withdraw entire balance (100โ†’0), deposit $50 (0โ†’50), self-transfer $25 (no net change but valid)

Constraints

  • n == balance.length
  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค balance[i] โ‰ค 1012
  • 1 โ‰ค account, account1, account2 โ‰ค n
  • 0 โ‰ค money โ‰ค 1012
  • At most 104 calls will be made to each function

Visualization

Tap to expand
Digital Bank SystemStep 1: ValidateCheck account numbers1 โ‰ค account โ‰ค nโœ“Step 2: Check BalanceVerify sufficient fundsbalance โ‰ฅ amountโœ“Step 3: ExecuteUpdate balancesReturn successโœ“Account Array ExampleAccount 1$1000Account 2$2000Account 3$1500Account 4$500Account 5$750Transfer $200O(1) Direct Array Access โ€ข Atomic Operations โ€ข Input Validation
Understanding the Visualization
1
Account Validation
First, check if the account number exists (between 1 and n) - like verifying a valid account number
2
Balance Verification
For withdrawals/transfers, ensure sufficient funds - like checking account balance before dispensing cash
3
Atomic Transaction
Update account balances simultaneously - like a secure database transaction that either completes fully or fails completely
Key Takeaway
๐ŸŽฏ Key Insight: The banking system achieves optimal performance through direct array indexing (O(1) access) combined with proper validation, making it suitable for high-frequency transaction processing.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
28.4K Views
Medium Frequency
~15 min Avg. Time
892 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