Event Stream Deduplicator
Beginner Mode
Problem Statement
Design a deduplication system for an event stream with a configurable time-to-live (TTL) window.
In distributed systems, the same event can arrive multiple times due to retries, network issues, or at-least-once delivery guarantees. A deduplicator detects and filters these duplicates within a time window, while allowing the same event ID to be processed again after the window expires.
Implement the Deduplicator class:
Deduplicator(int ttl)initializes the deduplicator with a TTL window. An event is considered a duplicate if the same event ID was processed within the lastttltime units (inclusive). Ifttlis 0, no deduplication is performed (every event is new).bool process(int timestamp, int eventId)processes an event. Returnstrueif this event is new (not seen within the TTL window), orfalseif it is a duplicate. Timestamps are guaranteed to be in non-decreasing order. The timestamp of the event is always updated on each call, whether it is new or a duplicate.
Additional information
0 <= ttl <= 10^61 <= timestamp <= 10^90 <= eventId <= 10^6- Timestamps across calls are in non-decreasing order.
- At most
50,000calls will be made toprocess.
Example 1:
Input:
["Deduplicator", "process", "process", "process", "process", "process"]
[[5], [1, 100], [2, 200], [3, 100], [7, 100], [8, 200]]
Output: [null, true, true, false, false, true]
Explanation:
process(1, 100)-- event 100 is new. Returns true.process(2, 200)-- event 200 is new. Returns true.process(3, 100)-- event 100 was seen at t=1, and 3-1=2 <= 5 (within TTL). Duplicate. Returns false.process(7, 100)-- event 100 was last updated to t=3, and 7-3=4 <= 5. Still within TTL. Duplicate. Returns false.process(8, 200)-- event 200 was seen at t=2, and 8-2=6 > 5. TTL expired. New. Returns true.
Example 2:
Input:
["Deduplicator", "process", "process", "process", "process"]
[[0], [1, 10], [2, 10], [3, 10], [4, 10]]
Output: [null, true, true, true, true]
Explanation: TTL is 0, so no deduplication happens. Every event is treated as new.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Google