Katkat

Flag Fraudulent Transactions by Amount and Travel Time

BloombergSoftware Engineer · Technical Screen

Last updated

This problem tests your ability to use hash maps for efficient lookups and filtering collections based on conditional business logic.

Problem Statement

You are given a list of strings transactions, where each string represents a transaction in the format: name,time,amount,city.

A transaction is considered fraudulent if:

  1. The amount exceeds 2000.
  2. It occurs within 60 minutes (inclusive, i.e., abs(time1 - time2) <= 60) of another transaction by the same person in a different city.

Write a function findFraudulentTransactions(transactions) that returns a list of all fraudulent transaction strings. The order of the output list should match the original order of the transactions provided in the input.

Input Constraints

  • transactions.length <= 1000
  • Each transaction string is valid and adheres to the specified comma-separated format.
  • Time is represented as a non-negative integer.

Examples

Example 1: Input: ["alice,20,800,mountains", "alice,50,100,mountains", "alice,51,100,riverside"] Output: ["alice,50,100,mountains", "alice,51,100,riverside"] Explanation: The first transaction is safe. The second and third are within 60 minutes and in different cities.

Example 2: Input: ["bob,100,3000,cityA"] Output: ["bob,100,3000,cityA"] Explanation: The amount exceeds 2000.

Edge Cases

  • Multiple transactions for the same person at the exact same time in different cities.
  • A user having only one transaction that is safe (amount <= 2000).
  • Duplicate transaction strings (if identical in all fields, they count as distinct entries in the original list).

Hints

Hint 1

Parse each string into a structured object or tuple to easily access the name, time, amount, and city.

Hint 2

Group transactions by name using a hash map to quickly find all other transactions made by the same person.

Hint 3

Iterate through the list and, for each transaction, check the amount rule first, then perform a nested loop or lookup against all other transactions associated with that specific name.

Follow-up questions

  • How would you optimize this solution if the number of transactions per user was extremely large (e.g., millions)?
  • If the time constraint were increased to 24 hours, would your logic require significant changes, or is it already scalable?
  • How would you modify the function to return the indices of the fraudulent transactions instead of the strings themselves?