EasyHash TableDesignData StreamUpdated Sep 2026

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

calls1 foo2 bar3 foo8 bar10 foo11 foo012345Green calls print, red calls are blocked because the samemessage came too soon.
Each message text has its own clock: it can print again only once 10 seconds have passed since its last print.

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

Example 1
Input["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"] [[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10,
Output[null, true, true, false, false, false, true]
Why"foo" is first printed at 1, so it is blocked until 11. At 3 and 10 it is still too early, so both return false; at 11 the gap is exactly 10, so it prints again. "bar" prints at 2, which blocks it until 12, so the call at 8 returns false.
Example 2
Input["Logger", "shouldPrintMessage", "shouldPrintMessage"] [[], [5, "hello"], [5, "hello"]]
Output[null, true, false]
WhyThe two calls happen at the same timestamp. The first prints, and the second is the same message at the same time, so the gap is 0 and it is dropped.
Example 3
Input["Logger", "shouldPrintMessage", "shouldPrintMessage"] [[], [0, "a"], [10, "a"]]
Output[null, true, true]
WhyThe gap is 10, and the rule allows printing when the gap is 10 or more, so the second call prints.

Asked At

3 companies

How to Think About It

1

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.

2

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.

3

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.

4

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):

  1. If message not in map: print (return true), store timestamp.
  2. 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

mapmessagelast printedfoo1bar2After the first two calls the map holds one entry permessage.mapmessagelast printedfoo1bar23 - 1 = 2, which is less than 10, so "foo" is notprinted and the map stays the same.mapmessagelast printedfoo11bar211 - 1 = 10, so "foo" prints and its stored time isupdated to 11.print if message is newor timestamp - last[message] >= 10then set last[message] = timestamp
The map remembers the last printed time for each message; a new call prints only if the gap is at least 10.
ComplexityTime O(1) per callSpace O(n)Each call does one hash map lookup and at most one write; the map holds one entry per distinct message seen, so space grows with the number of unique messages.

What Trips People Up in Real Interviews

1

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).

2

Using current time instead of the provided timestamp. The function receives a timestamp parameter -- use it, not time.time(). The caller controls the clock.

3

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.

4

Not handling the case where the message was never printed before. If the message is new, print it immediately regardless of timestamp.

5

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 time
class 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 False

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently 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 >).