> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Logger Rate Limiter Python Solution with Tests

> Tested Python solution for LeetCode 359 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 359, [Easy](/catalog/easy). Topics: [Design](/catalog/topics/design), [Hash Table](/catalog/topics/hash-table), [Data Stream](/catalog/topics/data-stream). [View on LeetCode](https://leetcode.com/problems/logger-rate-limiter/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 359   # by problem number
lcpy gen -s logger_rate_limiter   # by problem name
```

## Problem

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`.

### Examples

```
Input
["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]
[[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"], [11, "foo"]]
Output
[null, true, true, false, false, false, true]

Explanation
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
```

### Constraints

* `0 <= timestamp <= 10^9`
* Every `timestamp` will be passed in non-decreasing order (chronological order).
* `1 <= message.length <= 30`
* At most `10^4` calls will be made to `shouldPrintMessage`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/logger_rate_limiter/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/logger_rate_limiter/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Logger:
    # Time: O(1) per call
    # Space: O(m) for m distinct messages
    def __init__(self) -> None:
        self.ok_until: dict[str, int] = {}

    def should_print_message(self, timestamp: int, message: str) -> bool:
        if timestamp < self.ok_until.get(message, 0):
            return False
        self.ok_until[message] = timestamp + 10
        return True
```

## Complexity

| Time | Space |
| - | - |
| O(1) per call | O(m) for m distinct messages |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.