2043. Simple Bank System
Asked at Capital One
Difficulty
Medium
Time
O(1) per operation
Space
O(n)
Pattern
Array
Companies
1
Problem
Design a class that simulates a bank with n accounts. Account numbers run from 1 to n, but the starting balances are given in a 0-indexed array balance, where account i + 1 starts with balance[i] dollars.
Implement three operations. deposit(account, money) adds money to one account. withdraw(account, money) removes money from one account. transfer(account1, account2, money) moves money from one account to another. Each method returns true if it succeeded and false if it did not.
A transaction is valid only when both rules hold: every account number mentioned is between 1 and n, and the account that gives money (the one in withdraw, or account1 in transfer) holds at least money. If a transaction is not valid, return false and change nothing — no balance may move. Deposits have no balance rule, only the account-number rule.
Accounts are 1-indexed, the array is 0-indexed
Constraints
- ●
n == balance.length - ●
1 <= n, account, account1, account2 <= 10^5 - ●
0 <= balance[i], money <= 10^12 - ●
At most 10^4 calls will be made to each function transfer, deposit, withdraw
Examples
["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"]
[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]][null, true, true, true, false, false]["Bank", "deposit", "withdraw", "transfer"]
[[[0, 0]], [1, 5], [2, 3], [1, 2, 5]][null, true, true, false]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Capital One | Medium | View all Capital One questions → |
How to Think About It
Store balances in an array. Account numbers are 1-indexed, so account a lives at index a - 1.
Write one helper, valid(a), that checks 1 <= a <= n. Every operation starts by validating its account numbers.
For withdraw and transfer, check the balance before changing anything — a failed operation must leave all balances untouched.
Balances and amounts go up to 10^12, so use 64-bit integers in C++/Java.
Follow-up discussion: concurrency (locking the two accounts in a transfer in a consistent order to avoid deadlock).
Optimal Approach
State: bal (array of balances), n.
transfer(a, b, m): valid if both accounts exist and bal[a] >= m; then bal[a] -= m, bal[b] += m.deposit(a, m): valid if a exists; bal[a] += m.withdraw(a, m): valid if a exists and bal[a] >= m; bal[a] -= m.
Each returns whether it succeeded.
Time: O(1) per operation. Space: O(n).
One helper plus three small methods
What Trips People Up in Real Interviews
Forgetting the 1-indexed account numbers.
Partially applying a transfer (debiting before checking the destination exists).
Using 32-bit integers for balances up to 10^12.
Not validating the account in deposit — deposits into nonexistent accounts must fail.
Solution Code
O(1) per operation timeclass Bank:
def __init__(self, balance):
self.bal = balance
self.n = len(balance)
def _ok(self, a):
return 1 <= a <= self.n
def transfer(self, account1, account2, money):
if not (self._ok(account1) and self._ok(account2)) or self.bal[account1 - 1] < money:
return False
self.bal[account1 - 1] -= money
self.bal[account2 - 1] += money
return True
def deposit(self, account, money):
if not self._ok(account):
return False
self.bal[account - 1] += money
return True
def withdraw(self, account, money):
if not self._ok(account) or self.bal[account - 1] < money:
return False
self.bal[account - 1] -= money
return TrueFrequently Asked Questions
What is the Simple Bank System problem?
Simple Bank System asks you to implement transfer, deposit, and withdraw on an array of account balances, returning false for any invalid transaction (a nonexistent account or insufficient funds). It is a design warm-up about validation order and 1-indexed accounts.
How do you solve Simple Bank System?
The optimal approach runs in O(1) per operation time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Simple Bank System?
Simple Bank System is asked at Capital One. It is a medium difficulty problem.
What are common mistakes on Simple Bank System?
- Forgetting the 1-indexed account numbers.
- Partially applying a transfer (debiting before checking the destination exists).
- Using 32-bit integers for balances up to `10^12`.
- Not validating the account in `deposit` — deposits into nonexistent accounts must fail.