280. Logger Rate Limiter

A logger system receives a stream of messages along with their timestamps. Each unique message should be printed at most once every 10 seconds. If a message is printed at timestamp t, the same message cannot be printed again until timestamp t + 10 or later.

All messages arrive in chronological order, and multiple messages may arrive at the same timestamp.

Implement the Logger class:

  • Logger(): Initializes the logger object.
  • bool shouldPrintMessage(int timestamp, string message): Returns true if the message should be printed at the given timestamp, otherwise returns false.

Example 1:

Input:

["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]

[[], [1, "apple"], [3, "banana"], [5, "apple"], [12, "apple"]]

Output: [null, true, true, false, true]

Example 2:

Input:

["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]

[[], [2, "hello"], [4, "hello"], [12, "hello"], [15, "world"]]

Output: [null, true, false, true, true]

Now Your Turn!

Pick the correct output for the given input

Input:

["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]

[[], [1, "test"], [9, "test"], [10, "test"], [11, "test"]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= timestamp <= 109
  • Every timestamp is given in non-decreasing order (chronological order).
  • 1 <= message.length <= 30
  • At most 104 calls will be made to shouldPrintMessage.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Logger {
public:
Logger() {
// Your code goes here
}
 
bool shouldPrintMessage(int timestamp, string message) {
// Your code goes here
}
};
Test Case

Input:

Calls
Params