We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Simple Bank System

Number: 2169

Difficulty: Medium

Paid? No

Companies: Capital One, Coinbase, Airbnb, Dropbox, Goldman Sachs, Uber, PhonePe


Problem Description

Implement a simple bank system which supports deposit, withdraw, and transfer operations on accounts. The bank has n accounts (1-indexed), each with an initial balance provided by a 0-indexed array. Each operation must validate that the account number is valid and that sufficient funds exist for withdraw or transfer operations.


Key Insights

  • Maintain an array (or list) to store account balances.
  • Remember to convert between 1-indexed account numbers (used in operations) and 0-indexed data structure.
  • Validate each transaction by checking:
    • The account numbers (and both in transfers) are within valid range.
    • The account to withdraw or transfer from has enough balance to cover the money amount.
  • Each operation (deposit, withdraw, transfer) can then be processed in constant O(1) time.

Space and Time Complexity

Time Complexity: O(1) per operation as each operation requires a constant number of checks and arithmetic operations.
Space Complexity: O(n), where n is the number of bank accounts stored in the balance array.


Solution

The solution involves creating a Bank class that maintains the current balance in an array. Each method (deposit, withdraw, and transfer) first checks whether the provided account number(s) are valid (by ensuring the account numbers are between 1 and n). For withdraw and transfer operations, additionally check that the source account has a sufficient balance to handle the deduction. The operations are executed with simple arithmetic adjustments on the array values. This method guarantees constant-time operations and uses minimal extra space.


Code Solutions

class Bank:
    def __init__(self, balance):
        # Initialize the bank with the provided balance list.
        self.balance = balance

    def _is_valid(self, account):
        # Check if the account number is valid (1-indexed).
        return 1 <= account <= len(self.balance)

    def transfer(self, account1, account2, money):
        # Validate both account numbers.
        if not self._is_valid(account1) or not self._is_valid(account2):
            return False

        # Check if account1 has enough money.
        if self.balance[account1 - 1] < money:
            return False

        # Process the transfer.
        self.balance[account1 - 1] -= money
        self.balance[account2 - 1] += money
        return True

    def deposit(self, account, money):
        # Validate account number.
        if not self._is_valid(account):
            return False

        # Deposit money into the account.
        self.balance[account - 1] += money
        return True

    def withdraw(self, account, money):
        # Validate account number.
        if not self._is_valid(account):
            return False

        # Check if the account has enough money.
        if self.balance[account - 1] < money:
            return False

        # Withdraw money from the account.
        self.balance[account - 1] -= money
        return True

# Example usage:
# bank = Bank([10, 100, 20, 50, 30])
# print(bank.withdraw(3, 10))  # True
# print(bank.transfer(5, 1, 20)) # True
# print(bank.deposit(5, 20))     # True
# print(bank.transfer(3, 4, 15)) # False
# print(bank.withdraw(10, 50))   # False
← Back to All Questions