Last Updated: 5 Dec, 2021

Invalid Transactions

Moderate
Asked in company
Goldman Sachs

Problem statement

You are the manager of a bank. You have seen the database of the bank and have seen ‘N’ transactions. Each transaction has a customer id, transaction amount, time of the transaction, and the city code of the transaction. Now a transaction is invalid if the transaction amount exceeds Rs 1000 or the transaction occurs within 60 minutes of another transaction with the same customer id but in a different city. Find the number of invalid transactions.

Example:-
N = 3
TRANSACTIONS = [(1,20,100,1),(2,30,24,1),(1,60,90,2)]  [For the first transaction, customer_id is 1, the time of transaction is 20 minutes, the amount of transaction is Rs 100 and the city code where the transaction took place is 1, so the tuples are in the form (customer_id, time_fo_transaction, amount_of_transaction, city_code)].

ANSWER:- The invalid transactions are [(1,20,100,1),(1,60,90,2)] as the transactions are under the same customer id and have occurred in different cities within 60 minutes 
Input Format :
The first line contains a single integer ‘T’ representing the number of test cases. Then each test case follows.

The first line of every test case contains an integer ‘N’ denoting the number of transactions.

The next ‘N’ lines of every test case contain four integers ‘CUST_ID’, ‘TIME’, ‘AMOUNT’, ‘CITY’ denoting the customer id, time of the transaction, amount of transaction, and the city code of transaction respectively of the ith transaction.
Output Format :
For each test case, return the invalid transactions from the list of transactions.

The output of each test case should be printed in a separate line.
Note :
You are not required to print anything, it has already been taken care of. Just implement the function.    
Constraints :
1 <= T <= 10
1 <= N <= 10^3
1 <= CUST_ID <=10^9
1 <= TIME <= 10^9
1 <= AMOUNT <= 10^9
1 <= CITY <= 10^9

Time Limit = 1 sec

Approaches

01 Approach

Check if a transaction is valid by comparing it with all other transactions and if it’s invalid, add the transaction to the answer.

 

Algorithm:-

 

  1. Initialize an array named ANS to store the final answer.
  2. Iterate from 0 to N.(Let’s say the iterator is i)
    1. Iterate from 0 to N.(Let’s say the iterator be j).
      1. If the time difference between the ith transaction and the jth transaction is under 60 minutes and the transactions are in different cities, add the transaction to ANS.
      2. If the amount of the jth transaction exceeds Rs 1000, add the transaction to ANS.
  3. Return ANS.

02 Approach

For every pair of customer_id and time of the transaction, store the city in which the transaction occurs. After that check whether any transaction occurs within 60 mins in a different city with the same customer name.

 

Algorithm:-

 

  1. Initialize a dictionary named TRANSACTIONS to store the transactions with customer id and time of transaction wise.
  2. Iterate from 0 to N.(Let’s say the iterator is i)
    1. Add the transaction to TRANSACTIONS[(CUST_ID,TIME)].
  3. Initialize an empty array named ANS to store the final answer.
  4. Iterate from 0 to N.(Let’s say the iterator is i)
    1. Iterate from TIME - 60 to TIME + 60. (Let’s say the iterator be j).
      1. If AMOUNT is greater than 1000, add i to ANS and break.
      2. If a transaction with the current customer id and time j occurs and there is a different city apart from the current one, add i to ANS and break.
  5. Return ANS.