AlgoMaster Logo
AlgoMasterDeduplicate Stream Events with TTLmedium

Deduplicate Stream Events with TTL

medium

At-least-once delivery can replay the same event. A stream processor commonly remembers recently accepted event IDs and expires that state after a time-to-live period.

Design an EventDeduplicator class:

  • EventDeduplicator(int ttlSeconds) creates an empty deduplication store.
  • boolean accept(String eventId, int processingTime) returns whether this occurrence should be processed.

If an ID was last accepted at time t, reject it while processingTime < t + ttlSeconds. Accept it at or after the expiry boundary and replace its stored time. A rejected occurrence must not refresh the TTL. Calls use nondecreasing processing times, and IDs are case-sensitive.

Example 1:

The first a expires at time 10. Its accepted occurrence at 10 starts a new TTL, which blocks time 15.

Example 2:

The rejected calls at time 6 do not refresh state, so x becomes eligible at time 7.

Constraints

  • 1 <= ttlSeconds <= 10^9
  • 1 <= eventId.length <= 50
  • 0 <= processingTime <= 10^9
  • Processing times are nondecreasing for one object.
  • At most 10^4 calls are made to accept.
Hints

Loading...
CallReturns
new EventDeduplicator(10)null
accept("a", 0)true
accept("a", 9)false
accept("b", 9)true
accept("a", 10)true
accept("a", 15)false

The first a expires at time 10 and is accepted again there. Its new TTL then blocks a at time 15. Event b has independent state.

Run checks these cases. Submit also runs a larger hidden set.