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
What You Have:
โข
โข
Your Task:
Calculate the total monthly income (sum of all 'Creditor' transactions) for each account, then identify accounts where this total exceeds their
Example:
If Account 123 has
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 datesYour 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
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
โก Linearithmic
Space Complexity
O(k)
Only stores final result set, k = number of suspicious accounts
โ 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'
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code