You are given a list of transactions, where each transaction is [user_id, amount, merchant, timestamp]. A transaction may appear multiple times due to repeated authorizations (for example, a retry, a hold, and a final capture).
Two transactions are duplicates and must be merged if and only if they share the same user_id, amount, and merchant, and their timestamp values are within 60 seconds of each other (inclusive).
Merging is transitive: if A duplicates B and B duplicates C, then A, B, and C all collapse into a single transaction, even if A and C are more than 60 seconds apart. For each merged group, output [user_id, amount, merchant, earliest_timestamp] using the earliest timestamp in the group.
Return the merged transactions sorted by timestamp ascending, breaking ties by user_id ascending, then merchant ascending, then amount ascending.
Function Signature
def merge_transactions(transactions: list[list[str]]) -> list[list[str]]:
Rules
-
Every field is a string.
amount
and
timestamp
are decimal integers with no leading zeros.
-
Compare
timestamp
and
amount
as integers, and compare
user_id
and
merchant
as strings.
-
Two transactions with the same
user_id
,
amount
and
merchant
are within 60 seconds of each other when their timestamps differ by at most 60.
-
The input may be in any order, and identical transactions can appear more than once.
-
In the output,
amount
and the earliest timestamp are strings in the same format as the input.
Constraints
-
1 <= len(transactions) <= 10^5
-
Each transaction has exactly 4 fields.
-
1 <= len(user_id), len(merchant) <= 20
-
1 <= amount <= 10^6
-
0 <= timestamp <= 10^9
-
user_id
and
merchant
consist of lowercase English letters and digits.
Examples
Input: transactions = [["alice","50","amazon","100"],["alice","50","amazon","140"],["alice","50","amazon","300"],["bob","20","uber","100"]]
Output: [["alice","50","amazon","100"],["bob","20","uber","100"],["alice","50","amazon","300"]]
Alice's first two amazon charges are 40 seconds apart and merge into one transaction at 100. The charge at 300 comes 160 seconds after 140 and stands alone. Bob's charge is independent and ties with Alice's merged charge at 100, so user_id puts Alice first.
Input: transactions = [["u1","10","starbucks","1000"],["u1","10","starbucks","1050"],["u1","10","starbucks","1100"],["u1","10","starbucks","1200"]]
Output: [["u1","10","starbucks","1000"],["u1","10","starbucks","1200"]]
1000 and 1050 are 50 apart, and so are 1050 and 1100, so the first three chain into one group at 1000 even though 1000 and 1100 are 100 apart. The gap of 100 from 1100 to 1200 breaks the chain.
Input: transactions = [["carol","30","lyft","621"],["carol","30","lyft","500"],["carol","9","lyft","500"],["carol","30","lyft","560"]]
Output: [["carol","9","lyft","500"],["carol","30","lyft","500"],["carol","30","lyft","621"]]
For amount 30 the sorted timestamps are 500, 560 and 621. The gap of exactly 60 merges 500 and 560, and the gap of 61 starts a new group at 621. The two groups at 500 share the timestamp, user and merchant, so amount decides, and 9 comes before 30 as a number.