A consumer of Slack's Events API may receive the same event more than once. Given events in nondecreasing timestamp order, filter duplicates while keeping the first occurrence of each event_id within a rolling time window.
An event is a duplicate if its event_id has already been accepted and its timestamp is within the inclusive interval [current_timestamp - window, current_timestamp]. Events whose previous occurrence is older than this interval are accepted again. Preserve the input order of accepted events.
Implement deduplicate_events(events, window), where events is a list of dictionaries with string event_id, integer timestamp, and arbitrary payload fields. window is a nonnegative integer number of seconds. Return a list containing the original event dictionaries that are not duplicates.
Use a hash-based structure for fast membership checks and a queue to expire IDs that leave the rolling window.
def deduplicate_events(events, window):