DEV Community

Stylus07
Stylus07

Posted on

5 3

Logger Rate Limiter

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:

1. Logger() Initializes the logger object.
2. bool shouldPrintMessage(int timestamp, string message) Returns true if the message should be printed in the given timestamp, otherwise returns false.
Enter fullscreen mode Exit fullscreen mode

var Logger = function() {
    this.map = new Map();
};

Logger.prototype.shouldPrintMessage = function(timestamp, message) {
   if(this.map.has(message)){
       const prevTime = this.map.get(message);
       if(timestamp < prevTime + 10) return false;
   }
   this.map.set(message, timestamp);
   return true;
};
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →