Simple Bank System

Medium

Topics
ArrayHash TableDesignSimulation

You are given an array balance where balance[i] is the starting balance of account i + 1 — accounts are numbered from 1.

Bank(balance) creates the system. transfer(account1, account2, money), deposit(account, money) and withdraw(account, money) each return true if the transaction is valid and carry it out, or false and change nothing.

A transaction is invalid if any account number given does not exist, or if the account being debited holds less than money.

Example 1

returnsBank([10,100,20,50,30])withdraw(3, 10)truetransfer(5, 1, 20)truedeposit(5, 20)truetransfer(3, 4, 15)falsewithdraw(10, 50)false
Accounts are numbered from 1, so this system has accounts 1 to 5 and anything outside that range is invalid. A transaction also fails when the account being debited holds less than the amount, and a failed transaction leaves every balance untouched — a transfer must not debit before checking the recipient exists.
Calls run top to bottom; the arrow shows what each one returns.
Input:  operations = ["Bank","withdraw","transfer","deposit","transfer","withdraw"], values = [[[10,100,20,50,30]],[3,10],[5,1,20],[5,20],[3,4,15],[10,50]]
Output: [null,true,true,true,false,false]
Explanation: The fourth transfer fails because account 3 holds only 10 after its withdrawal. The last fails because account 10 does not exist.

Constraints

  • n == balance.length
  • 1 <= n, account, account1, account2 <= 10^5
  • 0 <= balance[i], money <= 10^12
  • At most 10^4 calls to each function.
Editorial — intuition, approach, complexity and pitfalls

Intuition

There is no clever structure here — the whole problem is validating before acting. Every operation has the same two failure modes: an account number out of range, and insufficient funds on the account being debited.

The one thing that is easy to get wrong is the off-by-one: accounts are numbered from 1 while the array is indexed from 0. Everything else follows from checking first and mutating second.

Approach

  1. Store the balances and remember how many accounts there are.
  2. Write one helper that answers whether an account number is in range, and use it everywhere rather than repeating the comparison.
  3. For transfer, validate both account numbers and the sender’s funds before moving anything — a partially applied transfer is worse than a rejected one.
  4. deposit only needs the account to exist; withdraw also needs sufficient funds.

Walkthrough

balance = [10, 100, 20, 50, 30]   (accounts 1..5)

withdraw(3, 10)     account 3 holds 20 -> true, now 10
transfer(5, 1, 20)  account 5 holds 30 -> true, 5 -> 10, 1 -> 30
deposit(5, 20)      exists -> true, 5 -> 30
transfer(3, 4, 15)  account 3 holds only 10 -> false
withdraw(10, 50)    account 10 does not exist -> false

Complexity

  • Time — O(1): every operation is a bounds check and at most two array updates.
  • Space — O(n): the balances, which are given.

Common Pitfalls

  • Accounts are 1-indexed and the array is 0-indexed — the single most common source of wrong answers here.
  • A failed transaction must leave every balance untouched, so validate fully before mutating; debiting then discovering the recipient does not exist loses money.
  • A transfer to the same account is valid and is a no-op in effect, not a failure.

Full Solution — all languages

class Bank:
    def __init__(self, balance):
        self.balance = list(balance)

    def _valid(self, account):
        return 1 <= account <= len(self.balance)

    def transfer(self, account1, account2, money):
        if not self._valid(account1) or not self._valid(account2):
            return False
        if self.balance[account1 - 1] < money:
            return False
        self.balance[account1 - 1] -= money
        self.balance[account2 - 1] += money
        return True

    def deposit(self, account, money):
        if not self._valid(account):
            return False
        self.balance[account - 1] += money
        return True

    def withdraw(self, account, money):
        if not self._valid(account) or self.balance[account - 1] < money:
            return False
        self.balance[account - 1] -= money
        return True
SensAI
Beta
Tap the mic to stop voice mode