Logger Rate Limiter
Description
Design a logger that decides whether a message should be printed.
A message should print if and only if it has NOT been printed in the
last 10 seconds. Timestamps arrive in non-decreasing order.
Methods:
__init__()
shouldPrintMessage(timestamp, message) -> bool
- True => print the message (and record this timestamp)
- False => suppress (don't update the recorded timestamp)
Example:
log = Logger()
log.shouldPrintMessage(1, "foo") -> True (first time)
log.shouldPrintMessage(2, "bar") -> True ("bar" never seen)
log.shouldPrintMessage(3, "foo") -> False (gap = 2 < 10)
log.shouldPrintMessage(11, "foo") -> True (gap = 10, allowed)
log.shouldPrintMessage(12, "foo") -> False (gap = 1 since t=11)
Edge to watch: when shouldPrint returns False you must NOT update the
stored timestamp. Otherwise repeated suppressed calls would slide the
window forward and never let the message through. Constraints
- Timestamps arrive in non-decreasing order - 'Last 10 seconds' means: print only if (timestamp - last_printed) >= 10 - Message strings are arbitrary
solution.py
output
Run the cases to see results here.