Logger Rate Limiter
Description
Design a logger system that receives messages with timestamps and returns true if the message should be printed. A repeated message can be printed again only when at least 10 seconds have elapsed since that same message was last printed. Return the result as an array.
Examples
shouldPrintMessage(1, 'foo'), shouldPrintMessage(2, 'bar'), shouldPrintMessage(3, 'foo')[true, true, false]'foo' prints at t=1 (first occurrence). 'bar' prints at t=2 (different message). 'foo' at t=3 is blocked because only 2 seconds passed since t=1, which is less than the 10-second cooldown.
shouldPrintMessage(15, 'hello'), shouldPrintMessage(20, 'world'), shouldPrintMessage(25, 'hello'), shouldPrintMessage(26, 'hello')[true, true, true, false]First 'hello' prints at timestamp 15. 'world' prints at 20 because it is a different message. The second 'hello' at 25 prints because exactly 10 seconds have elapsed since timestamp 15. The third 'hello' at 26 is blocked because only 1 second has elapsed since 'hello' was last printed at 25.
shouldPrintMessage(0, 'start'), shouldPrintMessage(5, 'start'), shouldPrintMessage(10, 'start'), shouldPrintMessage(11, 'start')[true, false, true, false]Same message 'start' is tested at different intervals. First prints at 0. At timestamp 5 it is blocked. At timestamp 10 it prints because exactly 10 seconds have elapsed. At timestamp 11 it is blocked because only 1 second has elapsed since the last printed 'start'.
Constraints
- •
0 ≤ timestamp ≤ 10⁹ - •
Timestamps are in non-decreasing order.