← Questions

Per-domain sliding-window rate limiter

0
GoogleCoding

A coding interview question asked at Google. It tests design, rate limiting and hash map.

A gateway receives requests one at a time, at most one per second. Each request names a domain. Build a rate limiter that keeps its state between calls and decides, for each request, whether to let it through.

Implement the DomainRateLimiter class:

class DomainRateLimiter:
    def __init__(self, limit: int, window: int) -> None: ...
    def should_allow(self, timestamp: int, domain: str) -> bool: ...

Rules

  • A domain may have at most limit accepted requests in any window seconds. A request at time t shares a window with earlier accepted requests at times in (t - window, t]. A request exactly window seconds older is outside the window.
  • should_allow returns True and records the request if the domain is under its limit. Otherwise it returns False.
  • A rejected request does not count toward the limit.
  • Domains are independent of each other.
  • timestamp is an integer number of seconds and never decreases between calls.

Constraints: 1 <= limit <= 1000, 1 <= window <= 10^5, up to 10^6 calls and 10^5 distinct domains. Aim for amortized O(1) per call.

Example 1

limiter = DomainRateLimiter(limit=2, window=5)
should_allow(1, "a.com")  -> True
should_allow(2, "a.com")  -> True
should_allow(3, "a.com")  -> False   # 2 accepted in (−2, 3]
should_allow(4, "b.com")  -> True    # other domain
should_allow(5, "a.com")  -> False   # 1 and 2 are still in (0, 5]
should_allow(6, "a.com")  -> True    # 1 has left the window

Example 2

limiter = DomainRateLimiter(limit=1, window=10)
should_allow(0, "x.io")   -> True
should_allow(9, "x.io")   -> False
should_allow(10, "x.io")  -> True    # 0 is not in (0, 10]

Be ready to discuss: limits that also apply to parent domains (api.x.io counts toward x.io), freeing memory for idle domains, and running the limiter on many threads or machines.

Clarifying questions

  • Is the at-most-one-request-per-second rule global or per domain? Can two calls share a timestamp?
  • Should domains be compared case-insensitively, or with a trailing dot stripped?
  • Does limit apply the same way to every domain, or can domains have their own limits?
  • How much memory is acceptable per domain, given up to 10^5 domains and limit up to 1000?

What a strong answer covers

  • Keeps a deque of accepted timestamps per domain. On each call it pops timestamps <= t - window from the front, accepts if fewer than limit remain, and appends t only when it accepts.
  • Explains why that is amortized O(1): each timestamp is appended and popped at most once.
  • Gets the boundary right: a request exactly window seconds older is outside, matching (t - window, t].
  • Notes the memory bound: at most limit timestamps per domain. Mentions the cheaper approximations (a fixed window counter, a sliding window counter, a token bucket) and what they give up.
  • Tests: both examples, a rejected request not counted, two domains interleaved, the exact window boundary, and a long run checked against a brute-force count.

Follow-ups

  • Limits that also apply to parent domains: check and record the request at each suffix (api.x.io, x.io), and accept only if every level is under its limit.
  • Free memory for idle domains, with a cleanup pass or a time-ordered structure of last-seen times.
  • Make it safe on many threads (a lock per domain), then across machines (a shared store such as Redis with an atomic script).
  • Return how long a rejected caller should wait before retrying.

Where it's asked

What it tests

design · rate limiting · hash map · queue

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding