Simple Bank System
Medium
Topics
You are given an array balance where balance[i] is the starting balance of account i + 1 — accounts are numbered from 1.
Bank(balance) creates the system. transfer(account1, account2, money), deposit(account, money) and withdraw(account, money) each return true if the transaction is valid and carry it out, or false and change nothing.
A transaction is invalid if any account number given does not exist, or if the account being debited holds less than money.
Example 1
Input: operations = ["Bank","withdraw","transfer","deposit","transfer","withdraw"], values = [[[10,100,20,50,30]],[3,10],[5,1,20],[5,20],[3,4,15],[10,50]] Output: [null,true,true,true,false,false] Explanation: The fourth transfer fails because account 3 holds only 10 after its withdrawal. The last fails because account 10 does not exist.
Constraints
- n == balance.length
- 1 <= n, account, account1, account2 <= 10^5
- 0 <= balance[i], money <= 10^12
- At most 10^4 calls to each function.
Editorial — intuition, approach, complexity and pitfalls
Intuition
There is no clever structure here — the whole problem is validating before acting. Every operation has the same two failure modes: an account number out of range, and insufficient funds on the account being debited.
The one thing that is easy to get wrong is the off-by-one: accounts are numbered from 1 while the array is indexed from 0. Everything else follows from checking first and mutating second.
Approach
- Store the balances and remember how many accounts there are.
- Write one helper that answers whether an account number is in range, and use it everywhere rather than repeating the comparison.
- For
transfer, validate both account numbers and the sender’s funds before moving anything — a partially applied transfer is worse than a rejected one. depositonly needs the account to exist;withdrawalso needs sufficient funds.
Walkthrough
balance = [10, 100, 20, 50, 30] (accounts 1..5)
withdraw(3, 10) account 3 holds 20 -> true, now 10
transfer(5, 1, 20) account 5 holds 30 -> true, 5 -> 10, 1 -> 30
deposit(5, 20) exists -> true, 5 -> 30
transfer(3, 4, 15) account 3 holds only 10 -> false
withdraw(10, 50) account 10 does not exist -> falseComplexity
- Time — O(1): every operation is a bounds check and at most two array updates.
- Space — O(n): the balances, which are given.
Common Pitfalls
- Accounts are 1-indexed and the array is 0-indexed — the single most common source of wrong answers here.
- A failed transaction must leave every balance untouched, so validate fully before mutating; debiting then discovering the recipient does not exist loses money.
- A transfer to the same account is valid and is a no-op in effect, not a failure.
Full Solution — all languages
class Bank:
def __init__(self, balance):
self.balance = list(balance)
def _valid(self, account):
return 1 <= account <= len(self.balance)
def transfer(self, account1, account2, money):
if not self._valid(account1) or not self._valid(account2):
return False
if self.balance[account1 - 1] < money:
return False
self.balance[account1 - 1] -= money
self.balance[account2 - 1] += money
return True
def deposit(self, account, money):
if not self._valid(account):
return False
self.balance[account - 1] += money
return True
def withdraw(self, account, money):
if not self._valid(account) or self.balance[account - 1] < money:
return False
self.balance[account - 1] -= money
return Trueimport java.util.*;
class Bank {
private final long[] balance;
public Bank(long[] balance) {
this.balance = balance.clone();
}
private boolean valid(int account) {
return account >= 1 && account <= balance.length;
}
public boolean transfer(int account1, int account2, long money) {
if (!valid(account1) || !valid(account2)) return false;
if (balance[account1 - 1] < money) return false;
balance[account1 - 1] -= money;
balance[account2 - 1] += money;
return true;
}
public boolean deposit(int account, long money) {
if (!valid(account)) return false;
balance[account - 1] += money;
return true;
}
public boolean withdraw(int account, long money) {
if (!valid(account) || balance[account - 1] < money) return false;
balance[account - 1] -= money;
return true;
}
}#include <bits/stdc++.h>
using namespace std;
class Bank {
public: Bank(vector<long long>& balance) : money_(balance) {
}
bool transfer(int account1, int account2, long long money) {
if (!valid(account1) || !valid(account2)) return false;
if (money_[account1 - 1] < money) return false;
money_[account1 - 1] -= money;
money_[account2 - 1] += money;
return true;
}
bool deposit(int account, long long money) {
if (!valid(account)) return false;
money_[account - 1] += money;
return true;
}
bool withdraw(int account, long long money) {
if (!valid(account) || money_[account - 1] < money) return false;
money_[account - 1] -= money;
return true;
}
private: vector<long long> money_;
bool valid(int account) {
return account >= 1 && account <= (int)money_.size();
}
};class Bank {
constructor(balance) {
this.balance = balance.slice();
}
valid(account) {
return account >= 1 && account <= this.balance.length;
}
transfer(account1, account2, money) {
if (!this.valid(account1) || !this.valid(account2)) return false;
if (this.balance[account1 - 1] < money) return false;
this.balance[account1 - 1] -= money;
this.balance[account2 - 1] += money;
return true;
}
deposit(account, money) {
if (!this.valid(account)) return false;
this.balance[account - 1] += money;
return true;
}
withdraw(account, money) {
if (!this.valid(account) || this.balance[account - 1] < money) return false;
this.balance[account - 1] -= money;
return true;
}
}