187. Design Logger System With Message Timestamps
Design Logger System With Message Timestamps
Design a Logger system that receives a stream of messages with timestamps.
Each message should be printed only if the same message was not printed during the previous 10 seconds.
Given a timestamp and a message, return true if the message should be printed at that timestamp. Otherwise, return false.
Multiple messages may arrive at the same timestamp.
Constructor and Method Signatures
Logger()
- Creates an empty logger system.
boolean shouldPrintMessage(int timestamp, String message)
- Returns
true if message should be printed at timestamp.
- Returns
false if the same message was already printed in the last 10 seconds.
- If the method returns
true, the message is considered printed at the given timestamp.
- If the method returns
false, the message is not printed and its last printed timestamp does not change.
Parameters
timestamp: The time when the message arrives, measured in seconds.
message: The message string received by the logger.
Return Value
- Return
true if the message should be printed.
- Return
false otherwise.
Printing Rule
A message can be printed at timestamp timestamp if either:
- The message has never been printed before.
- The message was last printed at time
lastPrintedTime and timestamp - lastPrintedTime ≥ 10.
If timestamp - lastPrintedTime < 10, the message must not be printed.
Constraints
0 ≤ timestamp ≤ 109
1 ≤ message.length() ≤ 100
message contains lowercase English letters only.
- Calls to
shouldPrintMessage are made with non-decreasing timestamps.
- No parameter value will be
null.
Examples
Example 1
Logger logger = new Logger()
logger.shouldPrintMessage(timestamp = 1, message = "apple")
Output: true
Explanation: "apple" has never been printed before, so it is printed.
logger.shouldPrintMessage(timestamp = 2, message = "banana")
Output: true
Explanation: "banana" has never been printed before, so it is printed.
logger.shouldPrintMessage(timestamp = 5, message = "apple")
Output: false
Explanation: "apple" was printed at timestamp 1, and only 4 seconds have passed.
logger.shouldPrintMessage(timestamp = 10, message = "apple")
Output: false
Explanation: "apple" was printed at timestamp 1, and only 9 seconds have passed.
logger.shouldPrintMessage(timestamp = 11, message = "apple")
Output: true
Explanation: "apple" was last printed at timestamp 1, and 10 seconds have passed.
Example 2
Logger logger = new Logger()
logger.shouldPrintMessage(timestamp = 3, message = "cat")
Output: true
Explanation: "cat" has not been printed before.
logger.shouldPrintMessage(timestamp = 3, message = "dog")
Output: true
Explanation: Multiple messages may arrive at the same timestamp, and "dog" has not been printed before.
logger.shouldPrintMessage(timestamp = 3, message = "cat")
Output: false
Explanation: "cat" was already printed at timestamp 3.
logger.shouldPrintMessage(timestamp = 13, message = "cat")
Output: true
Explanation: "cat" was last printed at timestamp 3, and exactly 10 seconds have passed.