359. Logger Rate Limiter

Logger Rate Limiter (LeetCode 359) 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.

How to solve Logger Rate Limiter

Logger Rate Limiter is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.

Work towards the solution in these steps:

  1. Consider using a data structure that can associate messages with their last printed timestamps efficiently.
  2. Think about how to quickly check if a message was printed within the last 10 seconds when a new timestamp arrives.
  3. Since timestamps are non-decreasing, you can optimize your storage by removing or updating entries that are older than 10 seconds relative to the current timestamp.

The reference solution runs in O(1) time and O(n) space.

The Logger Rate Limiter solution and editorial walks through the full approach with reference code.

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.

Examples

Example 1

Input: {"operations": ["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"], "arguments": [[], [1, "foo"], [2, "bar"], [3, "foo"]]}

Output: [null, true, true, false]

Explanation: 'foo' at 1 prints, 'bar' at 2 prints, but 'foo' at 3 is within 10 seconds so does not print.

Example 2

Input: {"operations": ["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"], "arguments": [[], [0, "edge"], [9, "edge"], [10, "edge"], [19, "edge"], [20, "edge"]]}

Output: [null, true, false, true, false, true]

Explanation: The message is printed at timestamps 0, 10, and 20 because each is at least 10 seconds after its previous print, while the calls at 9 and 19 are suppressed.

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) time and O(n) 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:

Logger Rate Limiter FAQ

What is the LeetCode number of Logger Rate Limiter?

Logger Rate Limiter is LeetCode problem 359, rated Easy.

What pattern is used to solve Logger Rate Limiter?

Logger Rate Limiter is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

What is the time complexity of Logger Rate Limiter?

The reference solution runs in O(1) time and O(n) space.

Which companies ask Logger Rate Limiter?

Logger Rate Limiter has been reported in interviews at Google, Apple, Amazon, Bloomberg, Microsoft, Oracle.

Preparing this workspace.