A service is flooding its logs with the same messages. Build a Logger class whose should_print(timestamp, message) returns True if the message should be printed at that time (in seconds) and False if not. Each message prints at most once every 10 seconds: once it prints at time t, the same message is blocked until time t + 10, when it can print again. A blocked attempt does not restart the clock. Timestamps never go backwards.
Example
logger = Logger()logger.should_print(1, "foo") # Truelogger.should_print(2, "bar") # Truelogger.should_print(3, "foo") # Falselogger.should_print(8, "bar") # Falselogger.should_print(10, "foo") # Falselogger.should_print(11, "foo") # True
What this question tests
Tests a small stateful class with a precise contract. The one detail that matters is when the clock restarts: only when a message actually prints. Store the time of every attempt instead and a message that keeps arriving is silenced forever.
More Python interview questions