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

# Number of Recent Calls Python Solution

> Tested Python solution for LeetCode 933 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 933, [Easy](/catalog/easy). Topics: [Design](/catalog/topics/design), [Queue](/catalog/topics/queue), [Data Stream](/catalog/topics/data-stream). [View on LeetCode](https://leetcode.com/problems/number-of-recent-calls/description/).

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

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

## Problem

You have a `RecentCounter` class which counts the number of recent requests within a certain time frame.

Implement the `RecentCounter` class:

* `RecentCounter()` Initializes the counter with zero recent requests.
* `int ping(int t)` Adds a new request at time `t`, where `t` represents some time in milliseconds, and returns the number of requests that has happened in the past `3000` milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range `[t - 3000, t]`.

It is **guaranteed** that every call to `ping` uses a strictly larger value of `t` than the previous call.

### Examples

```
Input
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
Output
[null, 1, 2, 3, 3]

Explanation
RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
```

### Constraints

* `1 <= t <= 10^9`
* Each test case will call `ping` with **strictly increasing** values of `t`.
* At most `10^4` calls will be made to `ping`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class RecentCounter:
    # Time: O(1) amortized per ping call
    # Space: O(W) where W is the number of requests in the current 3000ms window
    def __init__(self) -> None:
        self.requests: deque[int] = deque()

    # Time: O(1) amortized (each timestamp is appended and popped at most once)
    # Space: O(1) beyond the stored window
    def ping(self, t: int) -> int:
        self.requests.append(t)
        while self.requests[0] < t - 3000:
            self.requests.popleft()
        return len(self.requests)
```

## Complexity

| Time | Space |
| - | - |
| O(1) amortized per ping call | O(W) where W is the number of requests in the current 3000ms window |

## Tags


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