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 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 period

Example:
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
Logger Rate Limiter System📨 Message Stream(t=15, "System Error")(t=16, "User Login")(t=17, "System Error")🗂️ Hash Table Lookup"System Error" → t=10"User Login" → t=5O(1) lookup time!⏰ Time CheckCurrent: t=15Last printed: t=10Difference: 15-10 = 55 < 10 seconds❌ BLOCKED🎯 Key Algorithm Steps1Hash lookup: O(1)2Time comparison3Decision logic4Update & returnif (current_time - last_time >= 10) → Allow printing📊 Performance Analysis⚡ Time: O(1)Hash operationsare constant time💾 Space: O(m)m = unique messagesMuch better than O(n)!🚀 vs Brute Force: 1000x faster!No need to store entire message historyPerfect for high-frequency logging systems
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

n
2n
Linear Growth
Space Complexity
O(m)

Where m is the number of unique messages seen so far

n
2n
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
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
48.0K Views
High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen