Logger Rate Limiter

Allow a message at most once every 10 seconds — hash map of last-seen timestamps.

Easy
Time O(1) Space O(M)
LeetCode
2 min read
hash-map design

Problem#

Design a logger such that shouldPrintMessage(timestamp, message) returns true iff the same message has not been printed within the last 10 seconds.

Examples#

  • shouldPrintMessage(1, "foo")true. shouldPrintMessage(2, "bar")true. shouldPrintMessage(3, "foo")false. shouldPrintMessage(11, "foo")true.

Constraints#

  • Timestamps are non-decreasing.

Hints#

Hint
Map message -> earliest timestamp it can next be printed.

Approach#

Store nextAllowed[message]. Print iff timestamp >= nextAllowed[message]; if so, set nextAllowed[message] = timestamp + 10. Missing key defaults to 0, which permits the first print.

Solution#

Logger Rate Limiter
class Logger {
unordered_map<string, int> nextAllowed;
public:
Logger() = default;
bool shouldPrintMessage(int timestamp, string message) {
auto it = nextAllowed.find(message);
if (it != nextAllowed.end() && timestamp < it->second) return false;
nextAllowed[message] = timestamp + 10;
return true;
}
};

Editorial#

Recording “next allowed time” instead of “last printed time” saves an addition on every check — pure preference. Memory grows with the number of distinct messages ever seen; production code would garbage-collect entries older than 10 seconds via a sliding deque, but the spec doesn’t require it.

Complexity#

  • Time: O(1) per call.
  • Space: O(M) distinct messages.

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.