359. Design Logger Rate Limiter
Asked at Atlassian, Netflix, Roblox
Difficulty
Easy
Time
O(1) per call
Space
O(n)
Pattern
Hash Table
Companies
3
Problem
Design a logger that receives a stream of messages, each with a timestamp. A message may only be printed if the same message text has not been printed in the last 10 seconds. If a message was printed at time t, then the next time that exact message can be printed is t + 10 or later.
Messages arrive in non-decreasing timestamp order, and several messages can share the same timestamp. Different messages never block each other — only an identical message text does.
Implement a class Logger with a method shouldPrintMessage(timestamp, message). Return true if the message should be printed now, and false if it must be dropped. When you return true, remember this timestamp as the new "last printed" time for that message.
A 10-second gate per message
Constraints
- ●
0 <= timestamp <= 10^9 - ●
Timestamps are given in non-decreasing order - ●
1 <= message.length <= 30 - ●
At most 10^4 calls are made to shouldPrintMessage
Examples
["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]
[[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10,[null, true, true, false, false, false, true]["Logger", "shouldPrintMessage", "shouldPrintMessage"]
[[], [5, "hello"], [5, "hello"]][null, true, false]["Logger", "shouldPrintMessage", "shouldPrintMessage"]
[[], [0, "a"], [10, "a"]][null, true, true]Asked At
3 companies| Company | Difficulty | |
|---|---|---|
| Atlassian | Easy | View all Atlassian questions → |
| Netflix | Easy | View all Netflix questions → |
| Roblox | Easy | View all Roblox questions → |
How to Think About It
The simplest approach: a hash map where key = message, value = timestamp of last print. When a new message arrives, check if it's been >= 10 seconds since the last print. If yes, print and update. If no, discard.
Why a hash map works: you need O(1) lookup for "was this message printed recently?" The map stores exactly one timestamp per message. Old entries are naturally handled by the timestamp check.
Memory concern: over time, the map grows unbounded. In production, you'd periodically evict entries older than 10 seconds. For an interview, mention this as a follow-up optimization.
Edge cases: same message twice at same timestamp (return false), timestamp can be non-increasing (always check against last stored timestamp, not current time).
Optimal Approach
Use a hash map mapping message -> last_printed_timestamp.
For each call to shouldPrintMessage(timestamp, message):
- If message not in map: print (return true), store timestamp.
- If message in map: check if
timestamp - stored_timestamp >= 10. If yes, print and update. If no, don't print.
The hash map gives O(1) per call. Space is O(n) where n = number of unique messages.
Time: O(1) per call. Space: O(n).
One timestamp per message in a hash map
What Trips People Up in Real Interviews
Forgetting that the 10-second window is relative to the last printed time, not absolute. Message printed at t=5 can be reprinted at t=15, not t=12 (5+10=15).
Using current time instead of the provided timestamp. The function receives a timestamp parameter -- use it, not time.time(). The caller controls the clock.
Storing a list of timestamps per message instead of just the last one. You only need the most recent timestamp. Old timestamps are always expired.
Not handling the case where the message was never printed before. If the message is new, print it immediately regardless of timestamp.
Comparing with >= instead of >. The problem says "within the last 10 seconds." At exactly 10 seconds, the message should be printable. Use >= (not >).
Solution Code
O(1) per call timeclass Logger:
def __init__(self):
self.msg_time = {}
def shouldPrintMessage(self, timestamp, message):
if message not in self.msg_time or timestamp - self.msg_time[message] >= 10:
self.msg_time[message] = timestamp
return True
return FalseFrequently Asked Questions
What is the Design Logger Rate Limiter problem?
Design a logger system that receives a message and prints it if it was not printed within the last 10 seconds. Each unique message should only be printed at most once in a 10-second window. This tests your ability to design a time-based throttle using a hash map.
How do you solve Design Logger Rate Limiter?
The optimal approach runs in O(1) per call time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Design Logger Rate Limiter?
Design Logger Rate Limiter is asked at Atlassian, Netflix, Roblox. It is a easy difficulty problem.
What are common mistakes on Design Logger Rate Limiter?
- Forgetting that the 10-second window is relative to the last printed time, not absolute. Message printed at t=5 can be reprinted at t=15, not t=12 (5+10=15).
- Using current time instead of the provided timestamp. The function receives a timestamp parameter -- use it, not `time.time()`. The caller controls the clock.
- Storing a list of timestamps per message instead of just the last one. You only need the most recent timestamp. Old timestamps are always expired.
- Not handling the case where the message was never printed before. If the message is new, print it immediately regardless of timestamp.
- Comparing with >= instead of >. The problem says "within the last 10 seconds." At exactly 10 seconds, the message should be printable. Use >= (not >).