A transaction is possibly invalid if:
$1000, or;60 minutes of another transaction with the same name in a different city.You are given an array of strings transaction 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.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"] Output: ["alice,20,800,mtv","alice,50,100,beijing"] Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"] Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"] Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000transactions[i] takes the form "{name},{time},{amount},{city}"{name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.{time} consist of digits, and represent an integer between 0 and 1000.{amount} consist of digits, and represent an integer between 0 and 2000.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding invalid transactions involves checking every transaction against all other transactions to see if it is invalid. We'll compare each transaction against the others based on the rules for determining invalidity. Transactions are flagged as invalid if they violate these rules.
Here's how the algorithm would work step-by-step:
def find_invalid_transactions_brute_force(transactions):
invalid_transactions = []
number_of_transactions = len(transactions)
for i in range(number_of_transactions):
current_transaction = transactions[i].split(',')
current_transaction_name = current_transaction[0]
current_transaction_time = int(current_transaction[1])
current_transaction_amount = int(current_transaction[2])
current_transaction_city = current_transaction[3]
is_valid = True
# Check if the transaction amount exceeds the threshold.
if current_transaction_amount > 1000:
is_valid = False
for j in range(number_of_transactions):
if i == j:
continue
other_transaction = transactions[j].split(',')
other_transaction_name = other_transaction[0]
other_transaction_time = int(other_transaction[1])
other_transaction_city = other_transaction[3]
# Check for name collision in different cities within one hour.
if (current_transaction_name == other_transaction_name and\
current_transaction_city != other_transaction_city and\
abs(current_transaction_time - other_transaction_time) <= 60):
is_valid = False
break
if not is_valid:
invalid_transactions.append(transactions[i])
return invalid_transactionsThe goal is to find transactions that are either too large in amount or happen too close in time or location to other transactions by the same person. We can do this efficiently by organizing the transactions and then checking for invalid ones.
Here's how the algorithm would work step-by-step:
def invalid_transactions(transactions):
transactions_by_name = {}
invalid_transactions_list = []
for transaction in transactions:
name, time, amount, city = transaction.split(',')
time = int(time)
amount = int(amount)
if name not in transactions_by_name:
transactions_by_name[name] = []
transactions_by_name[name].append([time, amount, city, transaction])
for name in transactions_by_name:
transactions_by_name[name].sort()
transaction_list = transactions_by_name[name]
for i in range(len(transaction_list)):
time, amount, city, transaction = transaction_list[i]
# Flag transactions exceeding the amount limit as invalid.
if amount > 1000:
invalid_transactions_list.append(transaction)
# Iterate through other transactions to check proximity
for j in range(len(transaction_list)):
if i == j:
continue
other_time, other_amount, other_city, other_transaction = transaction_list[j]
# Check if transactions are within the time limit and different cities.
if abs(time - other_time) <= 60 and city != other_city:
if transaction not in invalid_transactions_list:
invalid_transactions_list.append(transaction)
if other_transaction not in invalid_transactions_list:
invalid_transactions_list.append(other_transaction)
return invalid_transactions_list| Case | How to Handle |
|---|---|
| Null or empty transactions array | Return an empty list since there are no transactions to validate. |
| Transaction string is malformed (missing fields, incorrect format) | Handle parsing errors gracefully, possibly skipping the invalid transaction string and logging the error or returning an error result. |
| Time values are negative or extremely large | Validate the time value range and handle out-of-bounds values, likely skipping the transaction or adjusting the timestamp. |
| Amount is zero or negative | Consider amount <= 0 as valid or invalid based on problem requirements; if invalid, flag the transaction. |
| A very large number of transactions with the same name | Ensure the data structure used to store transactions for a given name doesn't cause a performance bottleneck (e.g., use a balanced tree or hash map for efficient lookups). |
| Two transactions by the same person, different cities, at the same exact time | This should be treated as invalid according to the problem statement. |
| Transactions are already sorted by time | The solution should still work correctly and efficiently, avoiding unnecessary sorting if it's not required by the chosen approach. |
| Integer overflow when calculating time differences | Use long data type for storing and calculating time differences to prevent overflow. |