Logger Rate Limiter - Problem

Design a logger system that receives a stream of messages along with their timestamps. Each unique message should only be printed at most every 10 seconds (i.e. a message printed at timestamp t will prevent other identical messages from being printed until timestamp t + 10).

All messages will come in chronological order. Several messages may arrive at the same timestamp.

Implement the Logger class:

  • Logger() Initializes the logger object.
  • bool shouldPrintMessage(int timestamp, string message) Returns true if the message should be printed in the given timestamp, otherwise returns false.

Input & Output

Example 1 — Basic Logger Operations
$ Input: operations = ["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"], parameters = [[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"]]
Output: [null, true, true, false, false, false]
💡 Note: Logger() initializes. shouldPrintMessage(1, "foo") returns true (first time). shouldPrintMessage(2, "bar") returns true (first time). shouldPrintMessage(3, "foo") returns false ("foo" was printed 2 seconds ago, less than 10). shouldPrintMessage(8, "bar") returns false ("bar" was printed 6 seconds ago). shouldPrintMessage(10, "foo") returns false ("foo" was printed 9 seconds ago, which is still less than 10).
Example 2 — Same Timestamp Messages
$ Input: operations = ["Logger", "shouldPrintMessage", "shouldPrintMessage"], parameters = [[], [5, "hello"], [5, "hello"]]
Output: [null, true, false]
💡 Note: First "hello" at timestamp 5 is allowed. Second "hello" at same timestamp 5 is blocked because 5 - 5 = 0 < 10 seconds.
Example 3 — Exact 10 Second Boundary
$ Input: operations = ["Logger", "shouldPrintMessage", "shouldPrintMessage"], parameters = [[], [0, "test"], [10, "test"]]
Output: [null, true, true]
💡 Note: "test" at timestamp 0 is allowed. "test" at timestamp 10 is allowed because 10 - 0 = 10 seconds (not less than 10).

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

Visualization

Tap to expand
Logger Rate Limiter INPUT Message Stream (timestamp, msg) [1, "foo"] - First foo [2, "bar"] - First bar [3, "foo"] - foo at t=3 [8, "bar"] - bar at t=8 [10, "foo"] - foo at t=10 Timeline t=1 t=2 t=3 t=8 t=10 Rule: 10 sec cooldown ALGORITHM STEPS 1 Initialize HashMap map: msg --> last_print_time 2 Check Message If msg not in map: print OK 3 Check Cooldown If time - last >= 10: print OK 4 Update Map Store timestamp if printed HashMap State Trace Message Last Time Action "foo" 1 OK "bar" 2 OK "foo" 1 (3-1=2) BLOCK "bar" 2 (8-2=6) BLOCK "foo" 10 (10-1=9) OK FINAL RESULT Output Array [null, true, true, false, false, true] Detailed Results t=1 "foo" --> true (new) t=2 "bar" --> true (new) t=3 "foo" --> false (3-1<10) t=8 "bar" --> false (8-2<10) t=10 "foo" --> true (10-1>=9) Complexity Time: O(1) per call Space: O(n) messages Key Insight: Hash Map for O(1) Lookup Use a HashMap to store each message's last print timestamp. For each incoming message, check if (current_time - last_time) >= 10. If true or message is new, print and update map. This provides constant time lookup and update for the rate limiting check. TutorialsPoint - Logger Rate Limiter | Hash Map Optimization
Asked in
Google 25 Amazon 18 Microsoft 12 Facebook 15
187.0K Views
Medium Frequency
~15 min Avg. Time
2.2K 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