Simple Bank System
Asked at Capital One
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.
Asked At
| 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).
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
class 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 is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
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.