Pergunta de entrevista da empresa Sonar

Variant of LeetCode 1169. Invalid Transactions and then write a Unit Test to verify the solution. /* Fraud Detection: Find Suspicious Transactions A bank wants to detect fraudulent transactions. A transaction is suspicious if: - The same user makes two transactions within 100 seconds of each other. - A single transaction amount exceeds $10,000. Return a list of users who have made fraudulent transactions. Example Input: transactions = [ { "userId": "Alice", "amount": 5000, "timestamp": 10050 }, { "userId": "Alice", "amount": 2000, "timestamp": 10200 }, { "userId": "Bob", "amount": 12000, "timestamp": 10400 }, { "userId": "Alice", "amount": 6000, "timestamp": 10700 } ] Output: ["Alice", "Bob"] */

Resposta da entrevista

Sigiloso

8 de set. de 2025

Sort all transactions by their timestamp. Then iterate over the transactions and use a HashMap to store the most recent transaction time for each user, so when a new transaction comes in we can quickly check if it’s within 100 seconds of their last one. If the time gap is too short or the amount is over 10,000, we mark that user as suspicious and include them in the result. Keep a HashSet of suspicious users to efficiently avoid marking duplicate users, then convert to a list for final return.