LogIn
I don't have account.

Amazon Transaction Logs

Code Crafter
194 Views

Your Amazonian team is responsible for maintaining a monetary transaction service. The transactions are tracked in a log file.

A log file is provided as a string array where each entry represents a transaction to service. Each transaction consists of

  • sender_user_id: Unique identifier for the user that initiated the transaction. It consists of only digits with at most 9 digits.
  • recipient_user_id: Unique identifier for the user that is receiving the transaction. It consists of only digits with at most 9 digits.
  • amount_of_transaction: The amount of the transaction. It consists of only digits with at most 9 digits

The values are separated by a space. For example, "sender_user_id recipient_user_id amount_of_transaction".

Users that perform an excessive amount of transactions might be abusing the service so you have been tasked to identify the users that have a number of transactions over a threshold. The list of user ids should be ordered in ascending numeric value.

Example 1 :-
logs = ["88 99 200", "88 99 300", "99 32 100", " 12 12 15"]
threshold = 2
The transactions count for each user, regardless of role are:
ID     Transactions
--     ------------
99     3
88     2
12     1
32     1

There are two users with at least threshold = 2 transactions: 99 and 88. In ascending order, the return array is ['88', '99'].

Note : In the last log entry, user 12 was on both sides of the transaction. This counts as only 1 transaction for user 12.

Function Description

Complete the function processLogs in the editor below.

The function has the following parameter(s):

  • string logs[n]: each logs[i] denotes the ith entry in the logs
  • int threshold: the minimum number of transactions that a user must have to be included in the result
  • Returns: string[]: an array of user id's as strings, sorted ascending by numeric value

Constraints :

  • 1 ≤ n ≤ 10^5
  • 1 ≤ threshold ≤ n
  • The sender_user_id, recipient_user_id and amount_of_transaction contain only characters in the range ascii['0'-'9'].
  • The sender_user_id, recipient_user_id and amount_of_transaction start with a non-zero digit.
  • 0 < length of sender_user_id, recipient_user_id, amount_of_transaction ≤ 9.
  • The result will contain at least one element.

Approach

The approach to solve this problem, involves iterating through each log entry and for each user ID encountered, counting the number of times it appears across all logs.

Implementation Steps
  • Initialize an empty dictionary to store transaction counts.
  • Iterate through each log entry
    1. Split the log into components.
    2. If the sender and recipient are the same, increment the count for that user by 1.
    3. If they are different, increment the count for both users by 1.
  • After processing all logs, extract user IDs with counts ≥ threshold.
  • Sort the resulting list numerically.
C++
Java
C#
Python
#include <iostream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <set>
#include <algorithm>

using namespace std;

vector<string> processLogs(vector<string>&amp; logs, int threshold) {
    unordered_map<string, int> transactionCounts;
    for (const string&amp; log : logs) {
        istringstream iss(log);
        string sender, recipient, amount;
        iss >> sender >> recipient >> amount;
        transactionCounts[sender]++;
        if (sender != recipient)
            transactionCounts[recipient]++;
    }
    vector<string> result;
    for (const auto&amp; kv : transactionCounts) {
        if (kv.second >= threshold)
            result.push_back(kv.first);
    }
    sort(result.begin(), result.end(), [](const string&amp; a, const string&amp; b) {
        return stoi(a) < stoi(b);
    });
    return result;
}
int main() {
    vector<string> logs = {
        "88 99 200",
        "88 99 300",
        "99 32 100",
        "12 12 15"
    };
    int threshold = 2;
    vector<string> result = processLogs(logs, threshold);
    for (size_t i = 0; i < result.size(); ++i) {
        cout << result[i];
        if (i != result.size() - 1)
            cout << ", ";
    }
    cout << endl;
    return 0;
}
88, 99

Time Complexity : O(N+MlogM) , where M is the number of users meeting the threshold.

Space Complexity : O(K) , Where K is the number of unique users