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 - Hash Map Approach INPUT transactions array "alice,20,800,mtv" name: alice time: 20 amount: $800 city: mtv "alice,50,100,beijing" name: alice time: 50 amount: $100 city: beijing Time Difference |50 - 20| = 30 min (within 60 min!) Different cities: mtv != beijing ALGORITHM STEPS 1 Group by Name Create HashMap[name] = list HashMap: "alice" --> [trans1, trans2] 2 Check Amount > $1000 800 < 1000: OK, 100 < 1000: OK 3 Compare Same Name Check time diff & city For alice's transactions: time: |50-20| = 30 <= 60 city: mtv != beijing BOTH INVALID! 4 Collect Invalid Add to result set FINAL RESULT Invalid Transactions Found "alice,20,800,mtv" Invalid: same name, diff city "alice,50,100,beijing" Invalid: same name, diff city Output Array ["alice,20,800,mtv", "alice,50,100,beijing"] Length: 2 OK - Both transactions invalid Key Insight: A transaction is invalid if (amount > $1000) OR (within 60 min of another transaction with SAME name but DIFFERENT city). Using HashMap groups transactions by name for efficient O(n^2) pairwise comparison within each group. Both conditions trigger invalidity! TutorialsPoint - Invalid Transactions | Hash Map - Group by Name Approach
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