Loading...
Loading...
You are a backend engineer at a fintech company. Due to network instability, your payment service sometimes receives duplicate transaction requests. Your task is to build a deduplication system that identifies and filters out duplicate transactions before they are processed.
A transaction is represented as a string in the format: "userId:amount:timestamp". Two transactions are considered duplicates if they share the same userId and amount, and their timestamps differ by at most k seconds.
Given a list of transaction strings and an integer k, return a list of unique transaction IDs (0-indexed positions) that should be processed. If a transaction is a duplicate of a previously accepted transaction, it should be rejected. Transactions should be evaluated in the order they appear.
Input:
transactions: A list of strings, each in the format "userId:amount:timestamp" where userId is alphanumeric, amount is a positive integer (in cents), and timestamp is a non-negative integer (Unix seconds).k: An integer representing the deduplication window in seconds (inclusive).Output:
Input: transactions = ["user1:500:1000", "user2:300:1001", "user1:500:1005", "user1:500:1012"], k = 10
Output: [0, 1, 3]
Explanation:
user1:500:1000): No prior transaction — accepted.user2:300:1001): Different user — accepted.user1:500:1005): Same user+amount as transaction 0. |1005 - 1000| = 5 <= 10 — duplicate, rejected.user1:500:1012): Same user+amount. |1012 - 1000| = 12 > 10 — accepted (outside window).Input: transactions = ["alice:200:0", "alice:200:5", "alice:200:10"], k = 5
Output: [0, 2]
Explanation:
1 <= transactions.length <= 10^5 0 <= k <= 10^9 Each transaction string matches the format "userId:amount:timestamp" 1 <= userId.length <= 20 (alphanumeric) 1 <= amount <= 10^9 0 <= timestamp <= 10^9 Timestamps are not necessarily sorted
|5 - 0| = 5 <= 5|10 - 0| = 10 > 5. But what about transaction 1? It was rejected, so only compare against accepted transactions. |10 - 0| = 10 > 5 — accepted.Input: transactions = ["bob:100:50"], k = 100
Output: [0]
Explanation: Only one transaction — always accepted.
(userId, amount) key.':'.k, accept it and update the map.