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
Account Balance: Running TotalDeposit+20002021-11-07Balance2000Withdraw-10002021-11-09Balance1000Deposit+30002021-11-11Balance4000SQL Window FunctionSUM( CASE type WHEN 'Deposit' THEN amount ELSE -amount END) OVER ( PARTITION BY account_id ORDER BY day)
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

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for intermediate join results

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค account_id โ‰ค 1000
  • 1 โ‰ค amount โ‰ค 10000
  • type is either 'Deposit' or 'Withdraw'
  • Each (account_id, day) pair is unique
  • Balance never goes below 0
Asked in
Amazon 45 Google 32 PayPal 28 Stripe 21
45.0K Views
High Frequency
~15 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen