Account Balance - Problem
You're working at a digital bank and need to track real-time account balances after each transaction.
Given a table Transactions containing all bank transactions, calculate the cumulative balance after each transaction for every account.
Key Points:
- All accounts start with a balance of
0 - Deposits increase the balance
- Withdrawals decrease the balance
- The balance will never go below 0 (guaranteed)
Return the result ordered by account_id, then by day.
Table Schema
Transactions
| Column Name | Type | Description |
|---|---|---|
amount
|
int | Transaction amount (always positive) |
account_id
PK
|
int | Unique identifier for the account |
day
PK
|
date | Date of the transaction |
type
|
ENUM('Deposit', 'Withdraw') | Type of transaction |
Primary Key: (account_id, day)
Note: Each account has at most one transaction per day
Input & Output
Example 1 โ Multiple accounts with various transactions
Input Table:
| account_id | day | type | amount |
|---|---|---|---|
| Deposit | 2021-11-07 | 1 | 2000 |
| 2021-11-09 | 1 | Withdraw | 1000 |
| 2021-11-11 | Deposit | 3000 | 1 |
| 2021-12-07 | Deposit | 7000 | 2 |
| 2 | 7000 | 2021-12-12 | Withdraw |
Output:
| account_id | day | type | amount | balance |
|---|---|---|---|---|
| 1 | 2021-11-07 | Deposit | 2000 | 2000 |
| Withdraw | 1000 | 1000 | 2021-11-09 | 1 |
| Deposit | 1 | 2021-11-11 | 4000 | 3000 |
| 2 | 7000 | Deposit | 7000 | 2021-12-07 |
| Withdraw | 7000 | 0 | 2021-12-12 | 2 |
๐ก Note:
Account 1:
- 2021-11-07: Deposit +2000 โ Balance = 0 + 2000 = 2000
- 2021-11-09: Withdraw -1000 โ Balance = 2000 - 1000 = 1000
- 2021-11-11: Deposit +3000 โ Balance = 1000 + 3000 = 4000
Account 2:
- 2021-12-07: Deposit +7000 โ Balance = 0 + 7000 = 7000
- 2021-12-12: Withdraw -7000 โ Balance = 7000 - 7000 = 0
Example 2 โ Single account with deposits only
Input Table:
| account_id | day | type | amount |
|---|---|---|---|
| 500 | 2021-01-01 | 1 | Deposit |
| Deposit | 300 | 1 | 2021-01-15 |
| 1 | 200 | Deposit | 2021-02-01 |
Output:
| account_id | day | type | amount | balance |
|---|---|---|---|---|
| 2021-01-01 | 500 | 500 | Deposit | 1 |
| 300 | 1 | Deposit | 2021-01-15 | 800 |
| 1 | 2021-02-01 | Deposit | 1000 | 200 |
๐ก Note:
All transactions are deposits, so balance keeps increasing: 500 โ 800 โ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Partition by Account
Each account gets its own calculation window
2
Order by Date
Transactions processed chronologically within each account
3
Calculate Running Sum
Deposits add, withdrawals subtract from cumulative total
Key Takeaway
๐ฏ Key Insight: Window functions with PARTITION BY create separate running totals for each account, while ORDER BY ensures chronological accumulation.
Time & Space Complexity
Time Complexity
O(nยฒ)
For each row, we potentially join with all previous rows from same account
โ Quadratic Growth
Space Complexity
O(n)
Space for intermediate join results
โก Linearithmic Space
Constraints
-
1 โค account_id โค 1000 -
1 โค amount โค 10000 -
typeis either'Deposit'or'Withdraw' -
Each
(account_id, day)pair is unique - Balance never goes below 0
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code