Problem LeetCode 359. Logger Rate Limiter ↗ · NeetCode ↗· The solutions on this page are my own.

Logger Rate Limiter Visualizer

LeetCode 359: print a message only if the same message was not printed in the last 10 seconds. Three loggers get the same calls; watch how much each one keeps in memory.

The problem, step by step

Skip to the visualizer ↓

Print a message only if it was not printed in the last 10 seconds

Log messages arrive one by one, each with a timestamp. Print each message, unless the same message was printed less than 10 seconds ago. Decide for every call.

  1. What you get

    A stream of calls to shouldPrintMessage(timestamp, message). Timestamps are whole seconds and never go backwards. Several calls can share a timestamp.

  2. What you return

    For every call, true when the message should be printed now, false when it should be suppressed.

  3. The rules

    • A message printed at time t may be printed again at t + 10 or later. At t + 9 it is still suppressed.
    • Only a printed call starts the 10 second wait. A suppressed call does not restart it.
    • Every message text has its own wait. Different messages never block each other.
  4. Try it by hand

    Calls
    (1, "disk full"), (2, "timeout"), (5, "disk full"), (11, "disk full"), (12, "timeout"), (15, "disk full")
    Output
    true, true, false, true, true, false
    1. t = 1, "disk full": never printed before, so true. It may print again from t = 11.
    2. t = 2, "timeout": a different message with its own wait, so true. Next allowed at 12.
    3. t = 5, "disk full": 5 is before 11, so false. The wait still ends at 11, not at 15.
    4. t = 11, "disk full": exactly 10 seconds after it was printed at 1, so true. Next allowed at 21.
    5. t = 12, "timeout": 12 is not before 12, so true. Next allowed at 22.
    6. t = 15, "disk full": before 21, so false.
  5. Edge cases to check

    • Exactly 10 seconds later is allowed: the check is a difference of at least 10, not more than 10.
    • The same message twice in the same second: the first call prints, the second does not.
    • Memory: remembering every message ever seen grows without limit. With millions of distinct messages, old ones have to be dropped.
  6. The question behind the solutions

    Remembering the last print time of every message answers each call in O(1), but that memory only grows. Which messages can you safely forget, and how do you find them cheaply?

Three loggers

The six calls from above, sketched the way you would draw them in an interview. All three give true, true, false, true, true, false; they differ in what they remember and what they can forget. Then watch all three run below.

1. HashMap

Remembers: for every message ever printed, the next time it may print.

callst=1disk fullnewt=2timeoutnewt=5disk full5 < 11t=11disk full11 ≥ 11t=12timeout12 ≥ 12t=15disk full15 < 21the mapmessagenextdisk fulltimeout11211222print: next = t + 10suppress: next staysthe wait starts at a print, never at a suppressed callnothing is ever removed: memory only grows
  1. Look up the message. Not there, or t ≥ next? Print.
  2. On a print, set next = t + 10.
  3. Otherwise suppress, and leave next as it is.

The catch: O(1) per call, but a message that never repeats stays in the map forever.

2. Queue + set

Remembers: the prints of the last 10 seconds in order, and their messages in a set.

after t = 2(1, disk full)(2, timeout)set: disk full, timeoutt = 11: (1, disk full) is 10 s old, drop it, then push(1, disk full)(2, timeout)(11, disk full)t = 12: drop (2, timeout), then push (12, timeout)(11, disk full)(12, timeout)set: disk full, timeoutt = 15: disk full is in the set, so suppressonly prints from the last 10 s are kept
  1. Drop from the front every print that is 10 seconds old or more, and take its message out of the set.
  2. Message in the set? Suppress.
  3. Otherwise print, push (t, message) and add the message to the set.

The catch: memory stays at the last 10 seconds of prints, but one call can have a long queue front to clear after a burst.

3. Two maps

Remembers: two maps, cur and prev, each covering 10 seconds, and when cur started.

prevcurat t = 11: cur becomes prev, cur starts emptymessageprinteddisk full1timeout2messageprinteddisk full11timeout12t = 11: disk full last at 1 in prev, 11 − 1 = 10: printt = 12: timeout last at 2 in prev, 12 − 2 = 10: printt = 15: disk full last at 11 in cur, 15 − 11 = 4: suppressevery 10 s the old prev is thrown away wholeat most 20 s of messages in memory
  1. 10 seconds since cur started? cur becomes prev and a new empty cur starts. 20 or more? Clear both.
  2. Find the last print time in cur, else in prev. Less than 10 seconds ago? Suppress.
  3. Otherwise print and write the time into cur.

The catch: it keeps up to 20 seconds of messages instead of 10, in return for forgetting in O(1): the old map is dropped whole.

Three ways to solve it, step by step

Scenario
timestamp0
Call yourself
Time

Calls per messageshaded = the 10 s after a print, when the same message is blocked

printed (true) suppressed (false) 10 s cooldown

Consolewhat actually gets printed

HashMap

messagenext allowed

time O(1)space O(all distinct messages)

Queue + set

queue (time order)
set of recent messages

time O(1) amortizedspace O(printed in last 10 s)

Two maps

previous
current

time O(1)space O(printed in last ~20 s)

Complexity

VersionTime per callSpaceRemoves old entriesTrade-off
HashMapO(1)O(distinct messages ever)NeverSimplest; grows forever with unique messages
Queue + setO(1) amortizedO(printed in last 10 s)One by one, on each callTightest memory; two structures to keep in sync
Two mapsO(1)O(printed in last ~20 s)A whole map at once, every 10 sBounded memory with no per-entry cleanup

Hashing a message costs O(length of the message), which every version pays. This is a rate limiter with a limit of 1, keyed by message instead of by client.

Related in the Lab Rate Limiter, the same idea per client · Hit Counter

© 2026 Arman Ajabkhani · armantik.com · Privacy