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.
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.
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.
What you return
For every call, true when the message should be printed now, false when it should be suppressed.
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.
t = 1, "disk full": never printed before, so true. It may print again from t = 11.
t = 2, "timeout": a different message with its own wait, so true. Next allowed at 12.
t = 5, "disk full": 5 is before 11, so false. The wait still ends at 11, not at 15.
t = 11, "disk full": exactly 10 seconds after it was printed at 1, so true. Next allowed at 21.
t = 12, "timeout": 12 is not before 12, so true. Next allowed at 22.
t = 15, "disk full": before 21, so false.
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.
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.
Look up the message. Not there, or t ≥ next? Print.
On a print, set next = t + 10.
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.
Drop from the front every print that is 10 seconds old or more, and take its message out of the set.
Message in the set? Suppress.
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.
10 seconds since cur started? cur becomes prev and a new empty cur starts. 20 or more? Clear both.
Find the last print time in cur, else in prev. Less than 10 seconds ago? Suppress.
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
message
next 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
Version
Time per call
Space
Removes old entries
Trade-off
HashMap
O(1)
O(distinct messages ever)
Never
Simplest; grows forever with unique messages
Queue + set
O(1) amortized
O(printed in last 10 s)
One by one, on each call
Tightest memory; two structures to keep in sync
Two maps
O(1)
O(printed in last ~20 s)
A whole map at once, every 10 s
Bounded 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.