Skip to content
Onsite

Logger Rate Limiter

EasyP3Coding

Design a logger that receives a stream of messages with timestamps (in seconds) and prints each distinct message at most once every 10 seconds.

Implement the Logger class:

  • Logger() creates the logger.
  • shouldPrintMessage(timestamp, message) returns true if message should be printed at timestamp, otherwise false. A message printed at time t blocks the same message until time t + 10: a call at t + 9 returns false, a call at t + 10 returns true.

Timestamps never decrease between calls (several calls may share a timestamp). Only a call that returns true starts a new 10-second block; a rejected call does not extend it.

Example 1

Input
["Logger","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage"] [[],[1,"foo"],[2,"bar"],[3,"foo"],[8,"bar"],[10,"foo"],[11,"foo"]]
Output
[null,true,true,false,false,false,true]
Why
foo is blocked at 8 and 10 (printed at 1), allowed again at 11.

Constraints

  • 0 <= timestamp <= 10^9
  • Timestamps are non-decreasing.
  • 1 <= message.length <= 30
  • At most 10^4 calls.

Hints

Run checks the 2 visible tests, Submit all 6.