Skip to content

Practice Simple Bank System

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2043
LeetCode Problem

Simple Bank System

Problem Statement

Bank(balance) uses account numbers 1 through n, with initial amounts in the zero-based balance array. deposit(account,money), withdraw(account,money), and transfer(account1,account2,money) return success. Reject nonexistent accounts and withdrawals or transfers exceeding the source balance, leaving all balances unchanged. Practice approach: Validate every account and required balance before mutation. For transfer, both validations must succeed before debiting or crediting. Store balances in a sufficiently wide integer because amounts reach 10^12. Each operation is O(1). Test insufficient funds, invalid destination, and a transfer to the same account.

Source: Simple Bank System on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["Bank","transfer","withdraw","deposit","transfer","withdraw"]
[[[20,5]],[1,2,10],[1,11],[3,4],[2,2,15],[2,15]]
Output
[null,true,false,false,true,true]
Explanation

The first transfer leaves [10,15]. Failed operations change nothing, and a valid self-transfer preserves the balance.

Constraints

n == balance.length
1 <= n, account, account1, account2 <= 10^5
0 <= balance[i], money <= 10^12
At most 10^4 calls to each method.

Related LeetCode Problems

Practice more problems similar to Simple Bank System.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.