Per-domain sliding-window rate limiter
I was asked this
No reports yet. Sign in to say where you were asked it.
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
limitaccepted requests in anywindowseconds. A request at timetshares a window with earlier accepted requests at times in(t - window, t]. A request exactlywindowseconds older is outside the window. -
should_allowreturnsTrueand records the request if the domain is under its limit. Otherwise it returnsFalse. - A rejected request does not count toward the limit.
- Domains are independent of each other.
-
timestampis 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
limitapply the same way to every domain, or can domains have their own limits? - How much memory is acceptable per domain, given up to
10^5domains andlimitup to 1000?
What a strong answer covers
- Keeps a deque of accepted timestamps per domain. On each call it pops timestamps
<= t - windowfrom the front, accepts if fewer thanlimitremain, and appendstonly 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
windowseconds older is outside, matching(t - window, t]. - Notes the memory bound: at most
limittimestamps 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
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
