progress

~/problems / Streams & durability / Rolling windows and rate counters

Logger Rate Limiter

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy ~12 min

A broken service floods the monitoring screen with the same alert. Show an alert, then hide repeats of that exact text for 10 seconds.

Build AlertMuter() with should_show(t: int, message: str) -> bool: return True (show it) if message was never shown, or was last shown at s with s + 10 <= t; otherwise False. Hidden alerts don't restart the 10 seconds. t never decreases, and alerts can share a second.

m = AlertMuter()
m.should_show(1, "disk full")     # True
m.should_show(2, "cpu hot")       # True
m.should_show(3, "disk full")     # False  (shown at 1)
m.should_show(8, "cpu hot")       # False
m.should_show(11, "disk full")    # True   (1 + 10 <= 11)
m.should_show(11, "disk full")    # False  (just shown at 11)
m.should_show(12, "cpu hot")      # True

⭐ Bonus: scanning the history still passes; a speed test with 300,000 calls earns a star at O(1) average per call.

Show hint

per message you need one number: when it was last shown.

Topic: Rolling windows and rate counters. QPS over the last N seconds, hit counters, rate limiters.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc