Logger Rate Limiter

Logger Rate Limiter is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

Problem statement

You are designing a logger system that receives a stream of messages, each accompanied by a timestamp in seconds. Implement the Logger class:

- Logger() initializes the logger object. - bool shouldPrintMessage(int timestamp, string message) returns true if the message should be printed at the given timestamp, otherwise returns false.

A message should only be printed if it has not been printed in the last 10 seconds (including the current timestamp). Each call to shouldPrintMessage will have a non-decreasing timestamp.

Constraints: - 0 <= timestamp <= 10^9 - 1 <= message.length <= 30 - At most 10^4 calls will be made to shouldPrintMessage.

Return true if the message should be printed at the given timestamp, otherwise return false.

Topics and companies

Topics: Hash Table, Design, Data Stream.

Reported in interviews at Google, Apple, Amazon, Bloomberg, Microsoft, Oracle.

How to practise Logger Rate Limiter on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(1) per shouldPrintMessage call. Each lookup and update in unordered_map is O(1) on average. time and O(n), where n is the number of unique messages ever seen. Each unique message is stored in the map. space.

Open Logger Rate Limiter in the code editor, or read the Logger Rate Limiter editorial for a worked solution with its approach and complexity analysis.

Where Logger Rate Limiter sits in the DSA pattern sheet

Problems related to Logger Rate Limiter

Other problems that use the same Design (General/Specific) and Design Patterns techniques:

Opening the problem

Progress is saved as you study.