Logger Rate Limiter

Free with account Updated Nov 19, 2026 · 5 min read
On this page

    Problem statement

    Design a logger that receives a stream of (timestamp, message) calls in non-decreasing time order. should_print(timestamp, message) returns True if the message should be printed, and False if the same message was printed less than 10 seconds ago. A message printed at time t blocks repeats until time t + 10, when it may print again. Rejected calls don't count as printed.

    A small design question, often the warm-up before a rate-limiting discussion. The core is a few lines; the follow-ups (memory, many threads, many servers) are where the interview goes next. Original problem: LeetCode 359.

    Example

    Each printed message opens a 10-second quiet window for that message only. "disk full" at 3 is blocked; at 11 its window has ended.
    Each printed message opens a 10-second quiet window for that message only. "disk full" at 3 is blocked; at 11 its window has ended.
    should_print(1,  "disk full")      -> True
    should_print(2,  "login failed")   -> True
    should_print(3,  "disk full")      -> False    # printed at 1, too soon
    should_print(8,  "login failed")   -> False
    should_print(11, "disk full")      -> True     # 11 - 1 = 10, allowed again
    should_print(11, "disk full")      -> False    # just printed at 11
    should_print(12, "login failed")   -> True

    Constraints (assumed)

    • Timestamps are integers in seconds and never decrease.
    • Up to 10⁴ calls; messages are short strings.

    What this question tests

    • Picking the right state to store: the next allowed time per message.
    • Getting the boundary right (exactly 10 seconds later is allowed).
    • Knowing what not to update on a rejected call.

    Questions to ask first

    1. Is exactly 10 seconds later allowed? (Here: yes.)
    2. Do rejected calls reset the window? (Here: no.)
    3. Are timestamps in order? (Here: yes. Out-of-order input is a follow-up.)
    4. How many distinct messages? If unbounded, memory needs cleanup.
    5. Single-threaded? (Here: yes.)