Invalid Transactions - Problem
Invalid Transactions - Credit Card Fraud Detection
You're building a fraud detection system for a bank. A transaction is considered possibly invalid if:
1. High Amount: The transaction amount exceeds
2. Suspicious Location: It occurs within 60 minutes (inclusive) of another transaction with the same name but in a different city
Input: Array of transaction strings, each formatted as:
Output: List of all invalid transaction strings
Example: If Alice makes a $500 transaction in NYC at time 0, then another $600 transaction in LA at time 30, both transactions are invalid due to the location rule.
You're building a fraud detection system for a bank. A transaction is considered possibly invalid if:
1. High Amount: The transaction amount exceeds
$10002. Suspicious Location: It occurs within 60 minutes (inclusive) of another transaction with the same name but in a different city
Input: Array of transaction strings, each formatted as:
"name,time,amount,city"Output: List of all invalid transaction strings
Example: If Alice makes a $500 transaction in NYC at time 0, then another $600 transaction in LA at time 30, both transactions are invalid due to the location rule.
Input & Output
example_1.py β Basic Location Conflict
$
Input:
["alice,20,800,mtv","alice,50,100,beijing"]
βΊ
Output:
["alice,20,800,mtv","alice,50,100,beijing"]
π‘ Note:
Alice has transactions in mtv and beijing within 30 minutes (50-20=30 β€ 60), so both are invalid due to location conflict.
example_2.py β Amount and Location Rules
$
Input:
["alice,20,800,mtv","alice,50,1200,mtv"]
βΊ
Output:
["alice,50,1200,mtv"]
π‘ Note:
Only the second transaction is invalid because amount 1200 > 1000. Same city so no location conflict.
example_3.py β Multiple People
$
Input:
["alice,20,800,mtv","bob,50,1200,mtv","alice,75,800,beijing"]
βΊ
Output:
["bob,50,1200,mtv","alice,75,800,beijing","alice,20,800,mtv"]
π‘ Note:
Bob's transaction invalid due to amount. Alice's transactions invalid due to location conflict (|75-20|=55 β€ 60).
Visualization
Tap to expand
Understanding the Visualization
1
Collect Transactions
Parse transaction strings into structured data
2
Group by Customer
Organize transactions by customer name using hash map
3
Check Amount Rule
Flag any transaction over $1000 as high-risk
4
Detect Location Conflicts
Find impossible travel: same person, different cities, within 60 minutes
Key Takeaway
π― Key Insight: Group transactions by customer name to efficiently detect both high-amount violations and impossible travel patterns between different cities.
Time & Space Complexity
Time Complexity
O(nΒ²)
O(n) to parse and group, then O(kΒ²) for each group where k is transactions per person. Worst case: one person has all n transactions
β Quadratic Growth
Space Complexity
O(n)
Hash map to store transactions grouped by name
β‘ Linearithmic Space
Constraints
- 1 β€ transactions.length β€ 1000
-
Each transaction string has format:
name,time,amount,city - Name and city consist of lowercase English letters
- 1 β€ time β€ 1000
- 1 β€ amount β€ 2000
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code