Count Requests Within a Sliding Time Window
Data Structure DesignAuto-graded · 17 test casesLast updated
Tests ability to implement a sliding-window rate limiter by counting how many past requests fall within a fixed-duration window ending at each query timestamp.
Sliding-Window Request Counter
You are building a simple rate-limiter component. Given a log of request timestamps and a window size, determine how many prior requests (including the current one) are within the window for each request.
Task
Implement count_requests_in_window(timestamps, window) that takes:
timestamps— a non-decreasing list of integers, where each integer represents the arrival time of a request (in seconds). The same second may appear more than once.window— a positive integer representing the duration of the sliding window in seconds.
For each request at position i (0-indexed), count how many requests in timestamps[0..i] (inclusive) have a timestamp t satisfying:
timestamps[i] - window < t <= timestamps[i]
In other words, the window is left-exclusive, right-inclusive: it covers the half-open interval (timestamps[i] - window, timestamps[i]].
Return a list of integers of the same length as timestamps, where element i is the count for request i.
Constraints
0 <= len(timestamps) <= 20000 <= timestamps[i] <= 10^9timestampsis non-decreasing (i.e.timestamps[i] <= timestamps[i+1])1 <= window <= 10^9- If
timestampsis empty, return an empty list.
Examples
Example 1
Input: count_requests_in_window([1, 2, 3, 4, 5], 3)
Output: [1, 2, 3, 3, 3]
Example 2
Input: count_requests_in_window([10, 10, 10, 10], 1)
Output: [1, 2, 3, 4]
Example 3
Input: count_requests_in_window([], 5)
Output: []
Hints
Hint 1
For each index i, you need the leftmost index j such that timestamps[j] > timestamps[i] - window; the answer is i - j + 1.
Hint 2
Because timestamps is non-decreasing, you can use binary search (bisect_right) to find that leftmost index efficiently.
Hint 3
Alternatively, a two-pointer approach works: advance a left pointer whenever timestamps[left] <= timestamps[i] - window.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
For each request at index i with timestamp t = timestamps[i], we need to count all requests whose timestamps fall in the interval (t - window, t]. Since the list is non-decreasing, all qualifying requests form a contiguous subarray ending at index i.
We only need to find the leftmost index left such that timestamps[left] > t - window. The answer is then i - left + 1.
Finding left efficiently
Because timestamps is non-decreasing, we can use binary search:
left = bisect.bisect_right(timestamps, t - window, 0, i + 1)
bisect_right(arr, x) returns the first index where a value strictly greater than x would be inserted — exactly what we need (left-exclusive boundary).
An equivalent two-pointer approach would also work in O(n) total, but binary search per element is clean and O(n log n) overall, well within the 2 000-element constraint.
Correctness
- The interval is
(t - window, t], so we excludet - windowitself (left-exclusive).bisect_righton valuet - windowskips all occurrences of that exact value, giving us the correct left boundary. - For duplicate timestamps at the current position, they all satisfy
t' == t <= t, so they are always counted. - Empty input: the loop never executes and an empty list is returned.
Complexity
| Time | O(n log n) — one binary search per element |
| Space | O(n) — output list |
Reference Solution
from typing import List
import bisect
def count_requests_in_window(timestamps: List[int], window: int) -> List[int]:
result = []
for i, t in enumerate(timestamps):
# Find leftmost index where timestamp > t - window
left = bisect.bisect_right(timestamps, t - window, 0, i + 1)
result.append(i - left + 1)
return result