Logger Rate Limiter - Problem
Design a Logger Rate Limiter System
You need to design a smart logging system that prevents spam by rate-limiting messages. The system receives a continuous stream of log messages along with their timestamps, and each unique message should only be printed at most every 10 seconds.
Here's how it works:
• If a message is printed at timestamp
• All messages arrive in chronological order (timestamps are non-decreasing)
• Multiple different messages can arrive at the same timestamp
Your task: Implement the
•
•
Example:
You need to design a smart logging system that prevents spam by rate-limiting messages. The system receives a continuous stream of log messages along with their timestamps, and each unique message should only be printed at most every 10 seconds.
Here's how it works:
• If a message is printed at timestamp
t, the same message cannot be printed again until timestamp t + 10• All messages arrive in chronological order (timestamps are non-decreasing)
• Multiple different messages can arrive at the same timestamp
Your task: Implement the
Logger class with:•
Logger() - Initialize the logger•
shouldPrintMessage(timestamp, message) - Return true if the message should be printed, false if it's still in the cooldown periodExample:
logger.shouldPrintMessage(1, "foo") → true (first time)logger.shouldPrintMessage(2, "bar") → true (different message)logger.shouldPrintMessage(3, "foo") → false (too soon, need to wait until timestamp 11)logger.shouldPrintMessage(11, "foo") → true (cooldown period over) Input & Output
example_1.py — Basic Usage
$
Input:
Logger logger = new Logger();
logger.shouldPrintMessage(1, "foo");
logger.shouldPrintMessage(2, "bar");
logger.shouldPrintMessage(3, "foo");
›
Output:
true
true
false
💡 Note:
First "foo" at t=1 is allowed. "bar" at t=2 is a different message, so allowed. Second "foo" at t=3 is only 2 seconds after the first (need 10), so blocked.
example_2.py — Cooldown Expiry
$
Input:
Logger logger = new Logger();
logger.shouldPrintMessage(1, "foo");
logger.shouldPrintMessage(11, "foo");
logger.shouldPrintMessage(21, "foo");
›
Output:
true
true
true
💡 Note:
"foo" at t=1, then at t=11 (exactly 10 seconds later - allowed), then at t=21 (10 seconds after t=11 - allowed).
example_3.py — Multiple Messages
$
Input:
Logger logger = new Logger();
logger.shouldPrintMessage(0, "A");
logger.shouldPrintMessage(0, "B");
logger.shouldPrintMessage(1, "C");
logger.shouldPrintMessage(5, "A");
logger.shouldPrintMessage(10, "A");
›
Output:
true
true
true
false
true
💡 Note:
Different messages at same timestamp are allowed. "A" is blocked at t=5 (only 5 seconds since t=0) but allowed at t=10 (exactly 10 seconds later).
Visualization
Tap to expand
Understanding the Visualization
1
Message Arrives
A new message arrives with timestamp
2
Check History
Look up when this message was last printed
3
Calculate Difference
Check if enough time (≥10 seconds) has passed
4
Update & Return
If allowed, update timestamp and return true
Key Takeaway
🎯 Key Insight: We only need to track the MOST RECENT timestamp for each message type - not the entire printing history. This transforms an O(n) linear search into an O(1) hash table lookup!
Time & Space Complexity
Time Complexity
O(1)
Hash table lookup and update operations are O(1) on average
✓ Linear Growth
Space Complexity
O(m)
Where m is the number of unique messages seen so far
✓ Linear Space
Constraints
- 0 ≤ timestamp ≤ 109
- Every timestamp will be passed in non-decreasing order (chronological order)
- 1 ≤ message.length ≤ 30
- At most 104 calls will be made to shouldPrintMessage
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code