Logger Rate Limiter
The problem
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.
Input: ["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"] [[], [1, "apple"], [3, "banana"], [5, "apple"], [12, "apple"]] Output: [null, true, true, false, true]
Input: ["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"] [[], [2, "hello"], [4, "hello"], [12, "hello"], [15, "world"]] Output: [null, true, false, true, true]
Input: ["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"] [[], [1, "test"], [9, "test"], [10, "test"], [11, "test"]]
- 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.
cpp
class Logger {
public:
Logger() {
// Your code goes here
}
bool shouldPrintMessage(int timestamp, string message) {
// Your code goes here
}
};java
class Logger {
public Logger() {
// Your code goes here
}
public boolean shouldPrintMessage(int timestamp, String message) {
// Your code goes here
}
}python
class Logger:
def __init__(self):
# Your code goes here
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
# Your code goes herejavascript
class Logger {
constructor() {
// Your code goes here
}
shouldPrintMessage(timestamp, message) {
// Your code goes here
}
}csharp
public class Logger
{
public Logger()
{
// Your code goes here
}
public bool ShouldPrintMessage(int timestamp, string message)
{
// Your code goes here
return false;
}
}go
type Logger struct {
// Your fields here
lastPrinted map[string]int
}
func Constructor() Logger {
// Your initialization here
return Logger{
lastPrinted: make(map[string]int),
}
}
func (this *Logger) ShouldPrintMessage(timestamp int, message string) bool {
// Your code here
return false
}Stuck? Show a way to structure it+
- 01Clarify whether timestamps are monotonic
- 02Look up the message's next allowed time
- 03Reject if current time is earlier
- 04On acceptance store timestamp plus the window
Reference answer
Then expect these follow-ups
How can expired keys be cleaned up?
Tests: follow-up reasoning
How would per-user limits change the key?
Tests: constraint adaptation
Free to read · better with Enzo
Practice this out loud with Enzo
Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.
Next question