31Windows over a request log
31 Windows over a request log
Netflix software engineer candidates report LeetCode 3, the longest substring without a repeated character, in coding rounds (2 reports), once as the same question on an array of numbers (1 source), and it's on Netflix's LeetCode tag lists, the six-month list included (2 sources). The same lists carry the look-back version, the same value again within k positions (q-nfx-012, and Airbnb's list too, q-abnb-011), and Logger Rate Limiter (1 source). A prep site's guide to OpenAI's software engineer loop lists implementing a rate limiter (1 source), and an OpenAI candidate reports a coding round on chat session analytics over sliding windows (1 report). This is practice in that shape, set in one system, not a leaked question or LeetCode's version.
Set a 40-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/31_request_windows -k level1. Earlier levels must keep passing.
The log. An API gateway writes one entry per request: the key of the client that sent it, in the order the requests arrived. Keys are hashable values (strings in most tests). A run is a stretch of consecutive entries.
Level 1. Write longest_distinct_run(keys) -> tuple[int, int]. keys is a list. Return (start, length) for
the longest run in which no key appears twice, where start is the index of the run's first entry. When several
runs tie for longest, return the one that starts first. An empty log returns (0, 0).
["ana", "bo", "cy", "ana", "dee", "eli", "bo", "fay"] -> (2, 6)
The run from index 2 to index 7 holds six different clients. Make it one pass: O(n) for n entries. One test hands
you keys that count every time they're hashed or compared with ==, and it fails past 20 per entry.
Level 2. Quick repeats. A client that shows up again within a few requests is often a retry. Write
repeats_within(keys, k) -> list[int]: the positions, in order, of the entries whose key is also among the k
entries just before it, positions i - k to i - 1. k is at least 1. With the log above, k = 3 flags only
position 3, whose ana is exactly three entries after the one at 0, and k = 2 flags nothing. keys can now be any iterable, including a generator you can
walk only once, the way a live log arrives. Hold only the last k keys and what you need to know about them: one
test streams 100,000 different keys with k = 16 and fails if your code holds more than 1 MiB at once.
Level 3. A rate limiter. Write RateLimiter(limit, window) with allow(key, now) -> bool. A request from key
at time now is allowed when fewer than limit of that key's allowed requests still count at now. An allowed
request made at time t counts while t <= now < t + window, so it stops counting exactly window seconds later.
allow returns True and records the request, or returns False and records nothing: a denied request never
counts against later ones. The window slides with every call. It doesn't restart at round numbers: with a limit of
2 per 10 seconds, requests at 8 and 9 both count until 18 and 19. Times are seconds, ints or floats, and they never
go down from one call to the next. A limit below 1 or a window that isn't positive raises ValueError.
Keep the memory bounded by the requests that still count, not by every client you've ever seen. Forget a key once none of its requests count: one test sends one request each from 20,000 clients, about 10 of them inside any window, and fails if the limiter holds more than 1 MiB at once.
Things to get right: a left edge that only moves forward, a key that sits in the window twice, the exact edge where an old entry stops counting, and keys that leave memory when they leave the window.
Be ready for what comes next: the longest run when the rule is a sum and the numbers can be negative, the largest value in every window, a limit too big to keep a timestamp per request, and the same limit enforced across several gateway machines.