359. Logger Rate Limiter
Description
Design a logger system that receives a stream of messages along with their timestamps. Each unique message should only be printed at most every 10 seconds (i.e. a message printed at timestamp t
will prevent other identical messages from being printed until timestamp t + 10
).
All messages will come in chronological order. Several messages may arrive at the same timestamp.
Implement the Logger
class:
Logger()
Initializes thelogger
object.bool shouldPrintMessage(int timestamp, string message)
Returnstrue
if themessage
should be printed in the giventimestamp
, otherwise returnsfalse
.
Example 1:
1 | Input |
Constraints:
0 <= timestamp <= 10^9
- Every
timestamp
will be passed in non-decreasing order (chronological order). 1 <= message.length <= 30
- At most
10^4
calls will be made toshouldPrintMessage
.
Hints/Notes
- 2025/03/11 Q3
- design
- Leetcode solution
Step by step improvement:
- Use a queue of pairs + set, when there’s a new request coming in, we
remove items from the queue first(also remove item from set), then push
the item to the end of the queue. The problem: worst case time complexity
is bad due to inline cleanup - Use a unordered_map, so we can just check if the item is in the map. The
problem: need a separate job cleaning items - If size requirement comes in, then implement a LRU
- If the timestamp is un-ordered, then use a sorted data structure
Solution
Language: C++
1 | class Logger { |