SDE Path

Logger with Message Throttling

Easy

Logger with Message Throttling

A logger that suppresses duplicate messages inside a rolling window — the design question behind every 'why is my log 40GB' incident.

Requirements

  • A message may print again only window seconds after the last time it actually printed — suppressed attempts do not reset the clock.
  • Different messages throttle independently.
  • Timestamps arrive in non-decreasing order (a real log stream).

API to implement

Implement the class Logger. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | should_log <timestamp> <message> | boolean | Return true if message may be printed at timestamp — i.e. it was never printed, or was last printed at time p with timestamp ≥ p + window. Only a printed (true) call updates the message's record. Timestamps are non-decreasing. |

Input format

The first line contains Q, the number of operations. The second line contains the constructor argument: window — the throttle window in seconds. Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
7
10
should_log 1 foo
should_log 2 bar
should_log 3 foo
should_log 8 bar
should_log 10 foo
should_log 11 foo
should_log 12 bar
Expected output
true
true
false
false
false
true
true
Example 2
Input
5
5
should_log 0 x
should_log 4 x
should_log 5 x
should_log 9 x
should_log 10 x
Expected output
true
false
true
false
true