Bank Account Summary - Problem
Bank Account Summary
You're working for LeetCode Bank (LCB), a virtual payment system that helps programmers manage their finances. The bank maintains two key tables:
Users Table: Contains user information and their initial credit limit
Transactions Table: Records all money transfers between users
Your task is to calculate each user's current balance after all transactions and determine if they've breached their credit limit (balance goes below 0).
Goal: Generate a comprehensive bank account summary showing:
• User ID and name
• Original credit limit
• Current balance after all transactions
• Whether credit limit was breached ("Yes" or "No")
Think of it as creating a bank statement that shows if any account is overdrawn!
You're working for LeetCode Bank (LCB), a virtual payment system that helps programmers manage their finances. The bank maintains two key tables:
Users Table: Contains user information and their initial credit limit
Transactions Table: Records all money transfers between users
Your task is to calculate each user's current balance after all transactions and determine if they've breached their credit limit (balance goes below 0).
Goal: Generate a comprehensive bank account summary showing:
• User ID and name
• Original credit limit
• Current balance after all transactions
• Whether credit limit was breached ("Yes" or "No")
Think of it as creating a bank statement that shows if any account is overdrawn!
Input & Output
example_1.sql — Basic Bank Summary
$
Input:
Users table:
| user_id | user_name | credit |
|---------|-----------|--------|
| 1 | Moustafa | 100 |
| 2 | Jonathan | 200 |
| 3 | Winston | 10000 |
| 4 | Luis | 800 |
Transactions table:
| trans_id | paid_by | paid_to | amount | transacted_on |
|----------|---------|---------|--------|--------------|
| 1 | 1 | 3 | 400 | 2020-08-01 |
| 2 | 3 | 2 | 500 | 2020-08-02 |
| 3 | 2 | 1 | 200 | 2020-08-03 |
›
Output:
| user_id | user_name | credit | credit_balance | credit_limit_breached |
|---------|-----------|--------|--------------|-----------------------|
| 1 | Moustafa | 100 | -100 | Yes |
| 2 | Jonathan | 200 | 0 | No |
| 3 | Winston | 10000 | 9900 | No |
| 4 | Luis | 800 | 800 | No |
💡 Note:
User 1: Started with 100, paid 400 to user 3, received 200 from user 2. Final: 100 - 400 + 200 = -100 (breached). User 2: Started with 200, received 500 from user 3, paid 200 to user 1. Final: 200 + 500 - 200 = 500. User 3: Started with 10000, received 400 from user 1, paid 500 to user 2. Final: 10000 + 400 - 500 = 9900. User 4: No transactions, balance remains 800.
example_2.sql — No Transactions User
$
Input:
Users table:
| user_id | user_name | credit |
|---------|-----------|--------|
| 1 | Alice | 500 |
| 2 | Bob | 300 |
Transactions table:
| trans_id | paid_by | paid_to | amount | transacted_on |
|----------|---------|---------|--------|--------------|
| 1 | 1 | 2 | 100 | 2020-08-01 |
›
Output:
| user_id | user_name | credit | credit_balance | credit_limit_breached |
|---------|-----------|--------|--------------|-----------------------|
| 1 | Alice | 500 | 400 | No |
| 2 | Bob | 300 | 400 | No |
💡 Note:
Alice paid 100 to Bob. Alice: 500 - 100 = 400. Bob: 300 + 100 = 400. Neither user breached their credit limit.
example_3.sql — Multiple Breach Cases
$
Input:
Users table:
| user_id | user_name | credit |
|---------|-----------|--------|
| 1 | Charlie | 50 |
| 2 | David | 100 |
| 3 | Eve | 150 |
Transactions table:
| trans_id | paid_by | paid_to | amount | transacted_on |
|----------|---------|---------|--------|--------------|
| 1 | 1 | 2 | 100 | 2020-08-01 |
| 2 | 2 | 3 | 150 | 2020-08-02 |
| 3 | 3 | 1 | 50 | 2020-08-03 |
›
Output:
| user_id | user_name | credit | credit_balance | credit_limit_breached |
|---------|-----------|--------|--------------|-----------------------|
| 1 | Charlie | 50 | 0 | No |
| 2 | David | 100 | 50 | No |
| 3 | Eve | 150 | 250 | No |
💡 Note:
Charlie: 50 - 100 + 50 = 0 (exactly at limit). David: 100 + 100 - 150 = 50. Eve: 150 + 150 - 50 = 250. This shows edge case where balance equals 0 (not breached).
Visualization
Tap to expand
Understanding the Visualization
1
Initial State
Each user starts with their credit limit as initial balance
2
Transaction Processing
Money flows between users - outgoing reduces balance, incoming increases it
3
Balance Calculation
Final balance = Initial credit - Total paid + Total received
4
Breach Detection
If final balance < 0, the user has breached their credit limit
Key Takeaway
🎯 Key Insight: Use UNION ALL to create a single transaction stream with positive and negative effects, enabling efficient one-pass aggregation instead of multiple subqueries.
Time & Space Complexity
Time Complexity
O(n)
Single pass over transactions table with one GROUP BY
✓ Linear Growth
Space Complexity
O(n)
Space for aggregated transaction effects per user
⚡ Linearithmic Space
Constraints
- 1 ≤ Users.user_id ≤ 104
- 1 ≤ Transactions.trans_id ≤ 104
- 0 ≤ Users.credit ≤ 106
- 1 ≤ Transactions.amount ≤ 104
- All user_ids in Transactions exist in Users table
- Transactions.paid_by ≠ Transactions.paid_to (no self-payments)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code