359. Logger Rate Limiter
Practice Logger Rate Limiter for free on NudgeCode: Easy, Array & Hash Table pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Design a logger that receives a stream of messages with timestamps (in seconds) and suppresses repeats: a message may be printed only if the same message has not been printed in the last 10 seconds. Concretely, if a message was printed at time t, the same message is blocked until time t + 10.
shouldPrintMessage(timestamp, message)returnstrueand records the print if the message may be printed attimestamp, otherwise returnsfalse(a blocked message does not reset its timer).
Messages arrive in non-decreasing timestamp order; several messages can share a timestamp.
Each test is a list of operations and their arguments; the expected output lists what each call returns (null for the constructor).
Example
["Logger","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage"]
[[],[1,"foo"],[2,"bar"],[3,"foo"],[8,"bar"],[10,"foo"],[11,"foo"]]
-> [null,true,true,false,false,false,true]
Constraints
- 0 <= timestamp <= 10^9, non-decreasing across calls
- 1 <= message.length <= 30
- Up to 10^4 calls in total