Logger Rate Limiter

Easy

Topics
Hash TableDesignData Stream

Design a logger that limits each unique message to being printed at most once every 10 seconds. Implement Logger() and shouldPrintMessage(timestamp, message), which returns true if the message should be printed at the given timestamp (in seconds), and false otherwise. A message printed at time t may next be printed at t + 10 or later.

Example 1

Input:  operations = ["Logger","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage"], values = [[],[1,"foo"],[2,"bar"],[3,"foo"],[8,"bar"],[10,"foo"],[11,"foo"]]
Output: [null,true,true,false,false,false,true]

Constraints

  • 0 <= timestamp <= 10^9
  • Every timestamp is greater than or equal to the previous.
  • At most 10^4 calls.
Run ⌘' · Submit ⌘⏎