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 $1000
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: "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
🏦 Bank Fraud Detection SystemTransaction Processing PipelineRaw Dataalice,20,800,mtvalice,50,100,beijingbob,30,1500,nycGroup by NameAlice: [mtv,beijing]Bob: [nyc]Check RulesAmount > $1000?Location conflict?Flag InvalidAlice: Both txnsBob: High amountDetection LogicRule 1: Transaction amount > $1000 β†’ InvalidRule 2: Same person, different cities, |time₁ - timeβ‚‚| ≀ 60 β†’ Both invalidExample: Alice in MTV at time 20, Beijing at time 50|50 - 20| = 30 ≀ 60 minutes β†’ Impossible travel!
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

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

Hash map to store transactions grouped by name

n
2n
⚑ 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
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
28.4K Views
High Frequency
~25 min Avg. Time
856 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