← All Exercises

Deduplicate Events Inside a Time Window

intermediatejavascript~25min

Background

At-least-once delivery duplicates events. Windowed deduplication is the smallest mechanism that makes downstream counts correct.

Problem

Process a timestamp-ordered event stream and keep only the first event per window, reporting dropped duplicates.

Input

JSON object: { events: [{ id, ts }], windowSeconds }

Output

JSON object: { kept: string[], dropped: string[] }

Sample tests

Sample 1

Input: {"events":[{"id":"e1","ts":0},{"id":"e2","ts":1},{"id":"e3","ts":5}],"windowSeconds":2}
Expected: {"kept":["e1","e3"],"dropped":["e2"]}

Sample 2

Input: {"events":[{"id":"e4","ts":10},{"id":"e5","ts":20}],"windowSeconds":2}
Expected: {"kept":["e4","e5"],"dropped":[]}

Your solution

Learning objective

Make at-least-once delivery safe for downstream counters.