KatInterview

Duplicate Log Suppression Within a Cooldown Window

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-mapdesignsimulation

Last updated

Simulate a duplicate-log filter driven by an operation script, tracking per-message cooldowns where only accepted messages restart the timer.

Duplicate Log Suppression

A logging pipeline receives log lines, each with an integer timestamp (in seconds). To avoid flooding, it suppresses repeated identical lines that arrive too soon after the last one it let through.

The behavior of the filter is described as a script of operations, and your function must replay the script and report the result of each step.

The filter

  • Creating the filter sets a cooldown length window (in seconds).
  • Submitting a line (timestamp, message) yields a decision:
    • Accept (True) if this exact message string has never been accepted before, or if timestamp - last_accepted_time >= window, where last_accepted_time is the timestamp of the most recent accepted submission of that same string.
    • Reject (False) otherwise.
  • Accepting a line records its timestamp as the new last_accepted_time for that string.
  • Rejecting a line changes nothing. A rejected timestamp never restarts or extends a cooldown.
  • Different message strings are fully independent of each other. Several different messages may share the same timestamp, and the same message may be submitted more than once at one timestamp.
  • Message comparison is exact and case-sensitive.

Your task

Implement replay_filter(operations, arguments).

  • operations is a list of strings. Each is either "MessageLimiter" (create the filter) or "should_accept_message" (submit a line).
  • arguments is a list of the same length. For "MessageLimiter", the entry is [window], or an empty list [], in which case window is 10. For "should_accept_message", the entry is [timestamp, message].
  • Return a list of the same length as operations. The entry for a creation step is None; the entry for a submission is True or False per the rules above.
  • If "MessageLimiter" appears again later in the script, it creates a fresh filter: the new window applies and all earlier history is forgotten.
  • Submissions that appear before any creation step are treated as using a filter with window = 10 and empty history.
  • Any other operation name should raise ValueError.

Constraints

  • 1 <= window <= 10^6
  • 0 <= timestamp <= 10^9
  • Timestamps across successive submissions are non-decreasing.
  • 1 <= len(message) <= 50
  • At most 10^5 submissions in a script.
  • len(operations) == len(arguments)

Examples

Example 1

Input: replay_filter(['MessageLimiter', 'should_accept_message', 'should_accept_message', 'should_accept_message', 'should_accept_message', 'should_accept_message'], [[10], [1, 'foo'], [2, 'bar'], [3, 'foo'], [8, 'bar'], [10, 'foo']])

Output: [None, True, True, False, False, False]

Example 2

Input: replay_filter(['MessageLimiter', 'should_accept_message', 'should_accept_message', 'should_accept_message', 'should_accept_message'], [[5], [0, 'a'], [5, 'a'], [6, 'a'], [10, 'a']])

Output: [None, True, True, False, True]

Example 3

Input: replay_filter(['MessageLimiter', 'should_accept_message', 'should_accept_message', 'should_accept_message', 'should_accept_message', 'should_accept_message'], [[], [7, 'x'], [7, 'y'], [7, 'x'], [17, 'x'], [17, 'y']])

Output: [None, True, True, False, True, True]

Hints

Hint 1

For each distinct message you only need one number: when it was last accepted (or the earliest time it may be accepted again).

Hint 2

Only update that stored number when a submission is accepted. Updating on rejections would let a steady stream of repeats block a message forever.

Hint 3

The boundary is inclusive: a gap of exactly `window` is allowed. Use `in` or `.get(...) is None` to detect unseen messages, not a truthiness check, since timestamp 0 is valid.