You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has n
accounts numbered from 1
to n
. The initial balance of each account is stored in a 0-indexed integer array balance
, with the (i + 1)<sup>th</sup>
account having an initial balance of balance[i]
. Implement the Bank
class with the following methods:
Bank(long[] balance)
: Initializes the object with the 0-indexed integer array balance
.boolean transfer(int account1, int account2, long money)
: Transfers money
dollars from the account numbered account1
to the account numbered account2
. Return true
if the transaction was successful, false
otherwise.boolean deposit(int account, long money)
: Deposit money
dollars into the account numbered account
. Return true
if the transaction was successful, false
otherwise.boolean withdraw(int account, long money)
: Withdraw money
dollars from the account numbered account
. Return true
if the transaction was successful, false
otherwise.A transaction is valid if:
1
and n
, andFor example:
Bank bank = new Bank([10, 100, 20, 50, 30]);
bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10.
// Account 3 has $20 - $10 = $10.
bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20.
// Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30.
bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5.
// Account 5 has $10 + $20 = $30.
bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10,
// so it is invalid to transfer $15 from it.
bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.
Consider edge cases like invalid account numbers, insufficient balance, and potential overflow issues. How would you implement this Bank
class efficiently?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to managing bank accounts involves directly implementing each requested transaction by meticulously checking all accounts. We examine each account individually to determine if the transaction is possible, updating balances as needed. This method avoids any clever shortcuts or optimized data structures.
Here's how the algorithm would work step-by-step:
class Bank:
def __init__(self, balance):
self.account_balances = balance
def transfer(self, account1, account2, money):
account1 -= 1
account2 -= 1
# Validate both accounts exist
if account1 < 0 or account1 >= len(self.account_balances) or \
account2 < 0 or account2 >= len(self.account_balances):
return False
# Check if the first account has sufficient balance
if self.account_balances[account1] < money:
return False
self.account_balances[account1] -= money
self.account_balances[account2] += money
return True
def deposit(self, account, money):
account -= 1
# Check if the account exists
if account < 0 or account >= len(self.account_balances):
return False
self.account_balances[account] += money
return True
def withdraw(self, account, money):
account -= 1
# Need to validate account exists
if account < 0 or account >= len(self.account_balances):
return False
# Check to see if there is enough money to withdraw
if self.account_balances[account] < money:
return False
self.account_balances[account] -= money
return True
We'll represent bank accounts and their balances. We need to quickly access and modify balances while ensuring we don't try to access nonexistent accounts. The key is using an efficient way to store and retrieve account balances.
Here's how the algorithm would work step-by-step:
class Bank:
def __init__(self, balance):
self.account_balances = balance
def transfer(self, account1, account2, money):
# Validate both accounts before proceeding.
if account1 > len(self.account_balances) or \
account2 > len(self.account_balances):
return False
if self.account_balances[account1 - 1] < money:
return False
self.account_balances[account1 - 1] -= money
self.account_balances[account2 - 1] += money
return True
def deposit(self, account, money):
# Check if account exists.
if account > len(self.account_balances):
return False
self.account_balances[account - 1] += money
return True
def withdraw(self, account, money):
# Important to check sufficient funds first
if account > len(self.account_balances) or \
self.account_balances[account - 1] < money:
return False
self.account_balances[account - 1] -= money
return True
Case | How to Handle |
---|---|
Null or empty balance array | Return false immediately, as there are no accounts to operate on. |
Account number is zero or negative | Return false, as account numbers should be positive integers within the valid range. |
Account number exceeds the size of the balance array | Return false, as the account number is out of bounds. |
Withdrawal amount exceeds the account balance | Return false, as the transaction would result in a negative balance. |
Deposit or withdrawal amount is negative | Return false, as deposits and withdrawals should be non-negative. |
Transfer amount exceeds the sender's account balance | Return false, as the sender does not have sufficient funds. |
Transfer to an invalid or non-existent account | Return false if the recipient account number is invalid (out of bounds). |
Integer overflow when adding deposit amount or performing transfer calculations | Use long data type to prevent overflow, and add checks to ensure transaction amounts don't exceed maximum representable values. |