Suspicious Bank Accounts - Problem
Suspicious Bank Accounts Detection

You're working as a data analyst for a financial institution that needs to identify potentially suspicious bank accounts based on unusual income patterns.

The Problem:
A bank account becomes suspicious when its total monthly income exceeds the declared max_income for two or more consecutive months. This could indicate money laundering, tax evasion, or other fraudulent activities.

What You Have:
โ€ข Accounts table: Contains each account's ID and their declared maximum monthly income
โ€ข Transactions table: Contains all transactions with type ('Creditor' for deposits, 'Debtor' for withdrawals), amounts, and dates

Your Task:
Calculate the total monthly income (sum of all 'Creditor' transactions) for each account, then identify accounts where this total exceeds their max_income for 2+ consecutive months.

Example:
If Account 123 has max_income = 5000 but earned 6000 in January and 7000 in February, it's suspicious! ๐Ÿšจ

Input & Output

example_1.sql โ€” Basic Suspicious Account
$ Input: Accounts: [[3, 21000], [4, 10400]] Transactions: [[2, 3, 'Creditor', 107100, '2021-06-02'], [3, 4, 'Creditor', 10900, '2021-06-02'], [4, 4, 'Creditor', 1000, '2021-06-02']]
โ€บ Output: [3]
๐Ÿ’ก Note: Account 3 has max_income of 21000 but received 107100 in June 2021, exceeding the limit. However, we need to check if this continues for consecutive months to be truly suspicious.
example_2.sql โ€” Multiple Consecutive Months
$ Input: Accounts: [[1, 5000], [2, 3000]] Transactions: [[1, 1, 'Creditor', 6000, '2021-01-15'], [2, 1, 'Creditor', 7000, '2021-02-15'], [3, 2, 'Creditor', 2000, '2021-01-15']]
โ€บ Output: [1]
๐Ÿ’ก Note: Account 1: max_income=5000, but earned 6000 in Jan and 7000 in Feb (both exceed limit for 2 consecutive months = suspicious). Account 2: only 2000 in Jan, which is under the 3000 limit = not suspicious.
example_3.sql โ€” Edge Case: Non-consecutive Violations
$ Input: Accounts: [[1, 1000]] Transactions: [[1, 1, 'Creditor', 2000, '2021-01-15'], [2, 1, 'Creditor', 500, '2021-02-15'], [3, 1, 'Creditor', 3000, '2021-03-15']]
โ€บ Output: []
๐Ÿ’ก Note: Account 1 exceeded limits in Jan (2000 > 1000) and Mar (3000 > 1000), but Feb was within limits (500 < 1000). Since violations weren't consecutive, account is not suspicious.

Visualization

Tap to expand
Bank Account Monitoring SystemAccount 1001 | Max Income: $5,000/monthJan 2021$4,500โœ“ OKFeb 2021$6,200โš  OVERMar 2021$7,800โš  OVERApr 2021$3,100โœ“ OKCONSECUTIVE๐Ÿšจ ACCOUNT 1001 FLAGGED AS SUSPICIOUSExceeded max income for 2 consecutive months (Feb + Mar)๐Ÿ’ก Window functions efficiently detect these patterns across all accounts simultaneously
Understanding the Visualization
1
Monthly Income Calculation
Sum all 'Creditor' transactions per account per month
2
Limit Comparison
Compare monthly totals against max_income for each account
3
Consecutive Detection
Use window functions to check if violations occur in adjacent months
4
Suspicious Flagging
Mark accounts with 2+ consecutive monthly violations as suspicious
Key Takeaway
๐ŸŽฏ Key Insight: SQL window functions like LAG() allow us to compare current month with previous month in a single query, making consecutive violation detection both elegant and efficient!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single pass through data with sorting for window functions

n
2n
โšก Linearithmic
Space Complexity
O(k)

Only stores final result set, k = number of suspicious accounts

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค account_id โ‰ค 105
  • 1 โ‰ค max_income โ‰ค 106
  • 1 โ‰ค transaction_id โ‰ค 106
  • 1 โ‰ค amount โ‰ค 105
  • type is either 'Creditor' or 'Debtor'
  • day is in format 'YYYY-MM-DD HH:MM:SS'
Asked in
JPMorgan 45 Goldman Sachs 38 Bank of America 32 Wells Fargo 28 Citi 25
42.3K Views
High Frequency
~18 min Avg. Time
1.8K 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