Rate Limiter
You are given an array of timestamps representing API requests and need to implement a rate limiter that allows at most limit requests within a windowSize time period. The rate limiter should use a sliding window approach.
Function Signature
function rateLimiter(requests, limit, windowSize) { // Your code here }
Parameters
requests: An array of integers representing timestamps of API requestslimit: The maximum number of requests allowed within the windowwindowSize: The time window size in seconds
Return Value
Return an array of indices representing which requests should be allowed (0-indexed).
Examples
The outputs in the question are incorrect, but the explanations are mostly correct. Here are the corrected examples.
Example 1
Input
requests = [1, 2, 3, 4, 5] limit = 3 windowSize = 2
Output
[0, 1, 2, 3, 4]
Explanation
-
Request 0 at timestamp 1 → Allowed (queue:
[1]) -
Request 1 at timestamp 2 → Allowed (queue:
[1, 2]) -
Request 2 at timestamp 3
- Remove expired requests (
<= 3 - 2 = 1) - Remove timestamp 1
- Queue becomes
[2] - Allow request (queue:
[2, 3])
- Remove expired requests (
-
Request 3 at timestamp 4
- Remove expired requests (
<= 4 - 2 = 2) - Remove timestamp 2
- Queue becomes
[3] - Allow request (queue:
[3, 4])
- Remove expired requests (
-
Request 4 at timestamp 5
- Remove expired requests (
<= 5 - 2 = 3) - Remove timestamp 3
- Queue becomes
[4] - Allow request (queue:
[4, 5])
- Remove expired requests (
Since the queue size never reaches the limit after removing expired requests, all requests are allowed.
Example 2
Input
requests = [1, 2, 3, 4, 5] limit = 2 windowSize = 3
Output
[0, 1, 3, 4]
Explanation
-
Request 0 at timestamp 1 → Allowed (queue:
[1]) -
Request 1 at timestamp 2 → Allowed (queue:
[1, 2]) -
Request 2 at timestamp 3
- Remove expired requests (
<= 3 - 3 = 0) → None - Queue size is 2 (limit reached)
- Reject request
- Remove expired requests (
-
Request 3 at timestamp 4
- Remove expired requests (
<= 4 - 3 = 1) - Remove timestamp 1
- Queue becomes
[2] - Allow request (queue:
[2, 4])
- Remove expired requests (
-
Request 4 at timestamp 5
- Remove expired requests (
<= 5 - 3 = 2) - Remove timestamp 2
- Queue becomes
[4] - Allow request (queue:
[4, 5])
- Remove expired requests (
Constraints
0 ≤ requests.length ≤ 10001 ≤ limit ≤ 1001 ≤ windowSize ≤ 1000- All timestamps are non-negative integers
- Timestamps are in ascending order
Algorithm
- Use a queue to maintain the timestamps of allowed requests.
- For each request timestamp:
- Remove expired timestamps from the queue (
timestamps ≤ currentTime - windowSize). - If the queue length is less than the limit, allow the request.
- Add the current timestamp to the queue if allowed.
- Remove expired timestamps from the queue (
Test Cases
- Empty requests array
- Single request
- Multiple requests within a window
- Requests spanning multiple windows
- Edge cases with minimum/maximum values
Companies:
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
