Logger Rate Limiter
Easy
Topics
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
One timestamp per message is all the state needed: print when the last print was at least 10 seconds ago, and only update the timestamp when you actually print. Refreshing it on a rejected call would silently extend the block.
Calls run top to bottom; the arrow shows what each one returns.
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.
Editorial — intuition, approach, complexity and pitfalls
Intuition
For each message you only need the next timestamp at which it may be printed again. Store that in a hash map and compare against the incoming timestamp.
Approach
- Keep a map from message to the earliest allowed next timestamp.
- shouldPrintMessage returns true when the message is new or its allowed time has arrived, then records timestamp + 10.
- Otherwise it returns false without updating.
Dry Run
Logger()
t=1 foo no entry for foo -> true, last[foo] = 1
t=2 bar no entry for bar -> true, last[bar] = 2
t=3 foo 3 - 1 = 2, which is < 10 -> false, last unchanged
t=8 bar 8 - 2 = 6, which is < 10 -> false, last unchanged
t=10 foo 10 - 1 = 9, which is < 10 -> false, last unchanged
t=11 foo 11 - 1 = 10, NOT < 10 -> true, last[foo] = 11
answer null|true|true|false|false|false|true
t=10 is the row worth staring at. Nine seconds have passed since foo printed, not
ten, so it is still blocked; the test is timestamp - when < 10, and the boundary
at exactly 10 prints. Each message carries its own window, which is why bar being
rejected at t=8 does nothing to foo.
The update has to sit on the true branch only. Hoist last[message] = timestamp
above the check so every call refreshes it, and the window slides forward
indefinitely: foo's stamp becomes 3 at t=3, so t=11 is only 8 seconds later and
the run ends false instead of true.Complexity
- Time — O(1): each call is a single hash-map lookup and update.
- Space — O(m): one entry per distinct message seen.
Common Pitfalls
- Only update the allowed time when returning true, otherwise the window keeps sliding forward.
Full Solution — all languages
class Logger:
def __init__(self):
self.last = {}
def shouldPrintMessage(self, timestamp, message):
when = self.last.get(message)
if when is not None and timestamp - when < 10:
return False
self.last[message] = timestamp
return Trueimport java.util.*;
class Logger {
private final Map<String, Integer> last = new HashMap<>();
public Logger() {
}
public boolean shouldPrintMessage(int timestamp, String message) {
Integer when = last.get(message);
if (when != null && timestamp - when < 10) return false;
last.put(message, timestamp);
return true;
}
}#include <bits/stdc++.h>
using namespace std;
class Logger {
public: Logger() {
}
bool shouldPrintMessage(int timestamp, string message) {
auto it = last_.find(message);
if (it != last_.end() && timestamp - it->second < 10) return false;
last_[message] = timestamp;
return true;
}
private: unordered_map<string, int> last_;
};class Logger {
constructor() {
this.last = new Map();
}
shouldPrintMessage(timestamp, message) {
const when = this.last.get(message);
if (when !== undefined && timestamp - when < 10) return false;
this.last.set(message, timestamp);
return true;
}
}