Extend a basic rate limiter so that each request contains multiple fields such as userId, deviceId, or endpoint.
Each field dimension has its own rule of the form fieldName window limit. For the same field value, allow at most limit requests inside a window of length window seconds.
A request is ALLOWed only if it stays within the limit for every configured dimension. If any one dimension exceeds its limit, the request is REJECTed.
Complete the function applyMultiFieldRateLimiter in the editor below.
applyMultiFieldRateLimiter has the following parameters:
String[] fieldRules: each entry has the form"fieldName window limit"String[] requests: each entry has the form"timestamp value1 value2 ..."in the same order as the field rules
Returns
String[]: one result per request, each being ALLOW or REJECT.
fieldRules = ["user 10 2", "device 10 3"] requests = ["1 u1 d1", "2 u1 d1", "3 u1 d1", "4 u2 d1", "11 u1 d1", "12 u1 d1"] return = ["ALLOW", "ALLOW", "REJECT", "ALLOW", "ALLOW", "ALLOW"]
The third request is rejected because the user dimension exceeds its limit even though the device dimension is still valid.
fieldRules = ["user 5 1", "endpoint 3 2"] requests = ["1 u1 /a", "2 u1 /a", "2 u2 /a", "4 u2 /a"] return = ["ALLOW", "REJECT", "ALLOW", "REJECT"]
The second request fails the user rule, while the fourth request fails the endpoint rule because that endpoint already hit its limit in the active window.
1 <= fieldRules.length <= 101 <= requests.length <= 2 * 10^5- Each field value contains no spaces.
- Timestamps are non-decreasing.
- Implement a Rate LimiterPHONE SCREEN · Seen May 2026
- Find Number of Valid PairsSeen Sep 2024
- Schedule MeetingSeen Sep 2024
- Cyclic Shift PairsSeen Aug 2024
- Find Longest Diagonal SegmentSeen Aug 2024
- Make Towers Strictly Increasing or DecreasingSeen Aug 2024
- Find Max Number of PairsSeen Aug 2024
- Suffix PairsSeen Mar 2024
public String[] applyMultiFieldRateLimiter(String[] fieldRules, String[] requests) {
// write your code here
}