Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix

Logger Rate Limiter

easydesignhash-map

Problem

Design a logger that suppresses duplicate messages. A message may be printed only if the same message text has not been printed in the previous 10 seconds. Implement Logger with:

  • shouldPrintMessage(timestamp, message): return True if the message should be printed at this time (and record it), otherwise False.

Calls arrive with non-decreasing timestamps. A message printed at time t blocks the same text until time t + 10 inclusive of t + 9; at t + 10 it may print again.

  • Timestamps can grow into the billions of seconds.
  • Each message is a short string, at most a few dozen characters.
  • Expect up to about ten thousand calls.

Examples

  • (2, "warn") → True; (3, "error") → True; (4, "warn") → False; (9, "error") → False; (11, "warn") → False; (12, "warn") → True because 2 + 10 = 12.
  • (0, "a"), (0, "a") → True, then False; two calls at the same timestamp are still duplicates.
  • (6, "x") → True; (16, "x") → True; (25, "x") → False; the timer restarts from the last printed occurrence, not the last attempt.

Hints

  1. For each distinct message, what single number do you need to remember to answer the next query?
  2. Store the earliest time the message may print again. Only update it when you actually print.
  3. Hash map from message to next_allowed_time; print iff timestamp >= next_allowed_time, and on print set it to timestamp + 10.

Last reviewed 8 September 2026