Simple Bank System (LeetCode 2043): Solution and Explanation

Medium
Design
Array
Hash Table
Simulation

Implement transfer, deposit and withdraw on n numbered accounts, refusing any call that names a missing account or overdraws one.

Intuition

Accounts are numbered 1 to n with no gaps, so they map straight onto array indexes: account i is balance[i - 1]. No hash map is needed.

Every operation follows the same shape: check that the accounts exist, check that the paying account has enough money, and only then change balances.

Doing all checks first matters. If a transfer subtracted from account1 and then discovered that account2 does not exist, the bank would lose money.

Approach: Validate, then update

  1. Store the balances in an array (64-bit, since deposits can push a balance past 2^31).
  2. transfer(a, b, money): return false if a or b is outside 1..n or balance[a - 1] < money. Otherwise subtract from a, add to b, return true.
  3. deposit(a, money): return false if a is outside 1..n. Otherwise add and return true.
  4. withdraw(a, money): return false if a is outside 1..n or balance[a - 1] < money. Otherwise subtract and return true.

Step-by-step example

balance = [10, 100, 20, 50, 30]
  1. withdraw(3, 10): account 3 has 20 >= 10, so it drops to 10. Return true.
  2. transfer(5, 1, 20): both accounts exist and account 5 has 30 >= 20. Account 5 drops to 10, account 1 rises to 30. Return true.
  3. deposit(5, 20): account 5 exists, so it rises to 30. Return true.
  4. transfer(3, 4, 15): account 3 holds 10 < 15. Return false, nothing changes.
  5. withdraw(10, 50): there are only 5 accounts. Return false.
  6. Output: [null, true, true, true, false, false].

Code: Python, Java, C++

1class Bank:
2    def __init__(self, balance: List[int]):
3        self.balance = balance
4        self.n = len(balance)
5
6    def transfer(self, account1: int, account2: int, money: int) -> bool:
7        if not (1 <= account1 <= self.n and 1 <= account2 <= self.n):
8            return False
9        if self.balance[account1 - 1] < money:
10            return False
11        self.balance[account1 - 1] -= money
12        self.balance[account2 - 1] += money
13        return True
14
15    def deposit(self, account: int, money: int) -> bool:
16        if not 1 <= account <= self.n:
17            return False
18        self.balance[account - 1] += money
19        return True
20
21    def withdraw(self, account: int, money: int) -> bool:
22        if not 1 <= account <= self.n:
23            return False
24        if self.balance[account - 1] < money:
25            return False
26        self.balance[account - 1] -= money
27        return True
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1) per call

Each call does a fixed number of comparisons and array updates.

Space

O(n)

One balance per account.

Other approaches

Hash map of balances (time O(1) per call, space O(n))

Map account number to balance. It works the same way, but the dense numbering makes an array simpler and faster.

Common mistakes

  • Changing a balance before every check has passed, which leaves the bank in a half-done state on failure.
  • Forgetting the 1-based numbering and indexing balance[account] directly.
  • Storing balances as 32-bit ints. LeetCode allows balances up to 10^12, and even with small inputs repeated deposits can pass 2^31.
  • Treating a transfer from an account to itself as an error. It is valid as long as the account exists and has enough money.

Frequently asked questions

Is a transfer to the same account allowed?

Yes. If the account exists and holds at least money, subtracting and adding the same amount leaves it unchanged and the call returns true.

Is withdrawing 0 valid?

Yes, as long as the account exists. Every balance is at least 0, so the funds check passes.

Why does this version take int amounts?

LeetCode uses 64-bit balances and amounts up to 10^12. Here the inputs stay below 2^31 so the judge can pass them as ints, but the solution still keeps balances in 64-bit integers because sums can grow past that.