NewThe Learning module is now liveSee what's new

(This question is a variation of the LeetCode question 359. Logger Rate Limiter. If you haven't completed that question yet, it is recommended to solve it first.)

A gateway receives one domain request per second. Design a rate limiter that processes these requests one at a time and preserves its state between calls.

Implement the DomainRateLimiter class:

  • DomainRateLimiter() Initializes an empty rate limiter. The first request arrives at time 0.
  • String handleRequest(String domain) Processes a request for domain. Each call occurs one second after the previous call, including calls for rejected requests. Returns"{status: 200, message: OK}" for an accepted request and "{status: 429, message: Too many requests}" for a rejected request.

For each domain, accept at most two requests in any 5-second window and at most five requests in any 30-second window. Only accepted requests count toward later limits; rejected requests do not, and each domain is tracked independently.

Constraints:

  • 1 ≤ total calls to handleRequest ≤ 104
  • 1 ≤ domain.length() ≤ 15
  • Each domain contains only lowercase English letters and periods (.).

Example:

Input: ["DomainRateLimiter", "handleRequest", "handleRequest", "handleRequest", "handleRequest", "handleRequest", "handleRequest", "handleRequest", "handleRequest", "handleRequest"] [[], ["www.xyz.com"], ["www.abc.com"], ["www.xyz.com"], ["www.pqr.com"], ["www.abc.com"], ["www.xyz.com"], ["www.xyz.com"], ["www.abc.com"], ["www.xyz.com"]]

Output: [null, "{status: 200, message: OK}", "{status: 200, message: OK}", "{status: 200, message: OK}", "{status: 200, message: OK}", "{status: 200, message: OK}", "{status: 200, message: OK}", "{status: 429, message: Too many requests}", "{status: 200, message: OK}", "{status: 200, message: OK}"]

Explanation:

  • DomainRateLimiter limiter = new DomainRateLimiter(); // Initializes an empty rate limiter before time 0.
  • limiter.handleRequest("www.xyz.com"); // Returns "{status: 200, message: OK}" at time 0.
  • limiter.handleRequest("www.abc.com"); // Returns "{status: 200, message: OK}" at time 1.
  • limiter.handleRequest("www.xyz.com"); // Returns "{status: 200, message: OK}" at time 2.
  • limiter.handleRequest("www.pqr.com"); // Returns "{status: 200, message: OK}" at time 3.
  • limiter.handleRequest("www.abc.com"); // Returns "{status: 200, message: OK}" at time 4.
  • limiter.handleRequest("www.xyz.com"); // Returns "{status: 200, message: OK}" at time 5.
  • limiter.handleRequest("www.xyz.com"); // Returns "{status: 429, message: Too many requests}" at time 6 because the accepted requests at times 2 and 5 are both in the current 5-second window.
  • limiter.handleRequest("www.abc.com"); // Returns "{status: 200, message: OK}" at time 7.
  • limiter.handleRequest("www.xyz.com"); // Returns "{status: 200, message: OK}" at time 8 because the rejected request at time 6 was not counted.

Accepted
111/421
Acceptance Rate26.4%
Test Cases
Input
Output
Expected