Invalid Transactions - Problem

A transaction is possibly invalid if:

  • The amount exceeds $1000, or
  • It occurs within (and including) 60 minutes of another transaction with the same name in a different city

You are given an array of strings transactions where transactions[i] consists of comma-separated values representing the name, time (in minutes), amount, and city of the transaction.

Return a list of transactions that are possibly invalid. You may return the answer in any order.

Input & Output

Example 1 — Basic Invalid Transactions
$ Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
💡 Note: Both transactions are invalid: alice made transactions in different cities (mtv vs beijing) within 60 minutes (time difference = 30 minutes ≤ 60)
Example 2 — Amount Limit Violation
$ Input: transactions = ["alice,20,800,mtv","alice,50,1200,beijing"]
Output: ["alice,20,800,mtv","alice,50,1200,beijing"]
💡 Note: alice,50,1200,beijing is invalid due to amount > $1000. alice,20,800,mtv is invalid due to location conflict within 60 minutes
Example 3 — Valid Transactions
$ Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
💡 Note: alice's transaction is valid (amount ≤ 1000, no conflicts). bob's transaction is invalid due to amount > $1000

Constraints

  • transactions.length == n
  • 1 ≤ n ≤ 1000
  • Each transactions[i] takes the form "{name},{time},{amount},{city}"
  • Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10
  • Each {time} is given as integers between 0 and 1000
  • Each {amount} is given as integers between 0 and 2000

Visualization

Tap to expand
Invalid Transactions: Amount Limit + Location/Time ConflictsInput: ["alice,20,800,mtv", "alice,50,100,beijing", "bob,55,1200,mtv"]alice,20,800,mtvalice,50,100,beijingbob,55,1200,mtvAmount: 800 ≤ 1000 ✓Amount: 100 ≤ 1000 ✓Amount: 1200 > 1000 ✗Same person, different citiesTime diff: 50-20 = 30min ≤ 60min ✗INVALIDINVALIDINVALIDLocation conflictLocation conflictAmount violationOutput: ["alice,20,800,mtv", "alice,50,100,beijing", "bob,55,1200,mtv"]All three transactions are invalid for different reasons
Understanding the Visualization
1
Input
Array of transaction strings with name,time,amount,city format
2
Process
Check amount > 1000 OR same name in different cities within 60 minutes
3
Output
List of all transactions that violate either condition
Key Takeaway
🎯 Key Insight: A transaction is invalid if amount > $1000 OR same person appears in different cities within 60 minutes
Asked in
Amazon 12 Google 8 Microsoft 6 Facebook 4
28.5K Views
Medium Frequency
~25 min Avg. Time
892 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