Description
Company: Reddit
Problem Description
Design a logger system that receives a stream of timestamped messages. A message may be printed only if the same message has not been printed in the last 10 seconds.
Implement the Logger class:
Logger()Initializes the logger.boolean shouldPrintMessage(int timestamp, string message)Returnstrueif the message should be printed at timestamp, andfalseotherwise.
If a call returns true, record that print so the same message stays blocked until 10 seconds have passed. Timestamps arrive in non-decreasing order.
Examples
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]
Explanation:
Logger logger = new Logger();
logger.shouldPrintMessage(1, "foo"); // return true
logger.shouldPrintMessage(2, "bar"); // return true
logger.shouldPrintMessage(3, "foo"); // return false
logger.shouldPrintMessage(8, "bar"); // return false
logger.shouldPrintMessage(10, "foo"); // return false
logger.shouldPrintMessage(11, "foo"); // return true
Example 2:
Input: ["Logger","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage","shouldPrintMessage"] [[],[1,"foo"],[1,"bar"],[1,"foo"],[11,"foo"]]
Output: [null,true,true,false,true]
Explanation:
Each message has its own cooldown window. Repeating "foo" at the same timestamp is blocked, but "bar" is tracked independently.
Constraints
0 <= timestamp <= 10^91 <= message.length <= 30messageconsists of lowercase English letters.timestampvalues are in non-decreasing order.- At most
10^4calls will be made toshouldPrintMessage.
Company Notes
Follow-up: After the coding portion, interviewers may keep pushing on production concerns, such as how to handle high throughput, how to adapt the design for highly concurrent environments, and how you would test and deploy it.