Implement a Rate Limiter
Implement a rate limiter that decides whether each incoming request should be allowed at a given timestamp.
Each request contains a string key such as a user id, IP address, or API name, and a non-negative integer timestamp.
Given a time window of length window seconds and a maximum limit limit, allow at most limit requests for the same key inside any valid window. For each request, return whether it is ALLOWed or REJECTed.
Timestamps arrive in non-decreasing order. You may implement either a fixed-window or sliding-window policy, but the behavior must be consistent with the examples.
Complete the function applyRateLimiter in the editor below.
applyRateLimiter has the following parameters:
int window: the rate-limit window length in secondsint limit: the maximum number of allowed requests per key in one windowString[] requests: each entry has the form"timestamp key"
Returns
String[]: one result per request, each being ALLOW or REJECT.
1Example 1
The third request from alice arrives inside the same 10-second window as the first two and exceeds the limit. Later requests are evaluated against later windows.
2Example 2
Only one request per 5-second window is allowed for the same key in this example.
Constraints
Limits and guarantees your solution can rely on.
1 <= requests.length <= 2 * 10^51 <= window <= 10^91 <= limit <= 10^5- Timestamps are non-negative integers in non-decreasing order.
- Each request key contains no spaces.