You are given a list of trade events, where each event is a trade ID and a timestamp in seconds. A trade event is a duplicate if another event with the same trade ID occurred within the last window seconds (inclusive).
Process the events in order and return a list of booleans saying whether each event is a duplicate.
For the event at index i with trade ID id and timestamp t, it is a duplicate if there is an earlier event j (j < i) with the same trade ID and timestamp t_j such that t - t_j <= window.
Function Signature
def find_duplicate_trades(events: list[list], window: int) -> list[bool]:
...
Rules
-
Each event is a two-element list
[trade_id, timestamp]
, where
trade_id
is a string and
timestamp
is an integer.
-
Return a list with the same length as
events
. Entry
i
is
True
when event
i
is a duplicate and
False
otherwise.
-
Every earlier event counts, including events that were themselves duplicates.
-
Two events with the same trade ID and the same timestamp make the second one a duplicate, since the difference is 0.
Constraints
-
1 <= len(events) <= 10^5
-
trade_id
is a non-empty string of lowercase letters and digits
-
0 <= timestamp <= 10^9
-
Timestamps are given in non-decreasing order
-
1 <= window <= 10^9
Examples
Input: events = [["trade1", 100], ["trade2", 105], ["trade1", 108], ["trade3", 110], ["trade1", 115], ["trade2", 115]], window = 10
Output: [False, False, True, False, True, True]
"trade1" at 108 is 8 seconds after "trade1" at 100, and "trade1" at 115 is 7 seconds after "trade1" at 108. "trade2" at 115 is exactly 10 seconds after "trade2" at 105, which still counts because the window is inclusive.
Input: events = [["abc", 1], ["abc", 5], ["abc", 20], ["def", 21], ["def", 25]], window = 5
Output: [False, True, False, False, True]
"abc" at 5 is 4 seconds after "abc" at 1, so it is a duplicate. "abc" at 20 is 15 seconds after the previous "abc", which is outside the window, and "def" at 25 is 4 seconds after "def" at 21.