Logger Rate Limiter
EasyP3Coding
Design a logger that receives a stream of messages with timestamps (in seconds) and prints each distinct message at most once every 10 seconds.
Implement the Logger class:
Logger()creates the logger.shouldPrintMessage(timestamp, message)returnstrueifmessageshould be printed attimestamp, otherwisefalse. A message printed at timetblocks the same message until timet + 10: a call att + 9returnsfalse, a call att + 10returnstrue.
Timestamps never decrease between calls (several calls may share a timestamp). Only a call that returns true starts a new 10-second block; a rejected call does not extend it.
Example 1
- Input
- ["Logger","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage"] [[],[1,"foo"],[2,"bar"],[3,"foo"],[8,"bar"],[10,"foo"],[11,"foo"]]
- Output
- [null,true,true,false,false,false,true]
- Why
- foo is blocked at 8 and 10 (printed at 1), allowed again at 11.
Constraints
- 0 <= timestamp <= 10^9
- Timestamps are non-decreasing.
- 1 <= message.length <= 30
- At most 10^4 calls.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 2 visible tests, Submit all 6. ⌘Enter runs, ⌘⇧Enter submits.