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) Returns true if the message should be printed at timestamp, and false otherwise.
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.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
Each message has its own cooldown window. Repeating "foo" at the same timestamp is blocked, but "bar" is tracked independently.
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.