• Customer Support: admin@thealgorists.com
  • Feedback: We are listening to your every feedback, and taking action to constantly improve your learning experience. If you have any feedback, please use this form: https://thealgorists.com/Feedback.



Problem Statement:

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 the logger object.
bool shouldPrintMessage(int timestamp, string message) Returns true if the message should be printed in the given timestamp, otherwise returns false.

Example:
Logger logger = new Logger();
logger.shouldPrintMessage(1, "foo"); // return true, next allowed timestamp for "foo" is 1 + 10 = 11
logger.shouldPrintMessage(2, "bar"); // return true, next allowed timestamp for "bar" is 2 + 10 = 12
logger.shouldPrintMessage(3, "foo"); // 3 < 11, return false
logger.shouldPrintMessage(8, "bar"); // 8 < 12, return false
logger.shouldPrintMessage(10, "foo"); // 10 < 11, return false
logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, next allowed timestamp for "foo" is 11 + 10 = 21

Solution:

The discussion for this problem is going to be interesting. I would show you how you could come up with multiple solutions and go on optimizing them. The factors we consider while optimizing the solutions for this problem is very generic and applies to most data-intensive applications. So if you whatever you would be learning in this chapter, you should be able to apply that knowledge for most design problems.

Algorithm design, Low Level Design, doing great at technical interviews are all learnable skills. Once you get a hang of it, you would not only crack just one interview, but several ones. My mission is to help you get as much close to that state as possible. So please make sure you learn the thought process involved here: how we optimize solutions and do trade-offs, so that you can recreate your success in as many interviews you want. So, buckle up and enjoy! Happy learning!

1. Most Basic Solution:



This is a Premium Content.
Please subscribe to Low Level Design course to access the content.




2. Optimize Space Consumption:



This is a Premium Content.
Please subscribe to Low Level Design course to access the content.


3. O(1) Time and O(n) Space Complexity Solution using Two Maps:



This is a Premium Content.
Please subscribe to Low Level Design course to access the content.


4. Store messages only from last 10 seconds using concept of Hit Counter and Radix Sort:



This is a Premium Content.
Please subscribe to Low Level Design course to access the content.




The above content is written by:

Abhishek Dey

Abhishek Dey

A Visionary Software Engineer With A Mission To Empower Every Person & Every Organization On The Planet To Achieve More

Microsoft | University of Florida

View LinkedIn profile


If you have any feedback, please use this form: https://thealgorists.com/Feedback.




Subscribe to Our Youtube Channel

Follow Us On LinkedIn
wave