Presale Search Cap
When concert presale opens, fans hammer the seat search, and some scripts refresh it hundreds of times a minute. The ticket site limits each fan to a few searches per clock minute. The counter for the 10:04 minute starts at zero at 10:04:00 and is discarded at 10:05:00, and a fan who goes over the limit receives HTTP 429 "Too Many Requests".
This is the fixed-window counter from Rate Limiting. It keeps one small counter per user per window, which is cheap enough to hold for millions of users in a shared in-memory store. Its known weakness is at the window edge, as the second example shows.
Your task
Write a class WindowLimiter:
constructor(limit, windowSec)sets the rule that each fan may make at mostlimitallowed requests per window.allow(fan, t)handles a request fromfanat timet, in whole seconds. It returnstrueif the request is allowed andfalseif it is rejected.
Windows are fixed and aligned to 0: a request at time t falls in window number floor(t / windowSec), so with windowSec = 60 the windows are [0, 59], [60, 119], and so on. A request is allowed if the fan has had fewer than limit allowed requests in the same window. Rejected requests do not count. Each fan has separate counts.
class WindowLimiter
Examples
new WindowLimiter(3, 60)
.allow("ana", 10)
.allow("ana", 20)
.allow("ana", 30)
.allow("ana", 59)
.allow("ana", 60)[null, true, true, true, false, true]
Times 10, 20, 30 and 59 are in window 0 (0 to 59). The first three use up the limit of 3, so 59 is rejected. Time 60 starts window 1 with a fresh count.
new WindowLimiter(2, 60)
.allow("ben", 58)
.allow("ben", 59)
.allow("cy", 59)
.allow("ben", 60)
.allow("ben", 61)[null, true, true, true, true, true]
Ben makes 2 requests at 58 and 59 in window 0, then 2 more at 60 and 61 in window 1. That is 4 requests in 4 seconds even though the limit is 2 per minute, and this burst at the window edge is the cost of fixed windows. Cy has her own count, so her request at 59 is allowed.
Constraints
- 1 ≤ limit ≤ 1,000
- 1 ≤ windowSec ≤ 86,400
- 0 ≤ t ≤ 1,000,000,000, and t never decreases from one call to the next
- fan is a non-empty string of up to 20 letters and digits
- At most 2,000 calls per test
Start with brute force
The brute force stores every allowed request time for each fan and counts the stored times in the same window on each request. With 2,000 calls that is up to 4 million checks, and the stored history grows without bound.
Only the current window matters, so keeping a single counter per fan makes both the time and the memory per fan constant.
Hints
0 of 4 shown- Hint 1
For one fan, which earlier requests can affect whether the request at time
tis allowed? Do requests from an earlier window matter? - Hint 2
Only the current window matters, so each fan needs two numbers: the window being counted and the number of allowed requests in it.
- Hint 3
Keep a hash map from fan to
[window, count]. When a request arrives in a different window than the stored one, reset the count to 0 for the new window. - Hint 4
Compute
w = floor(t / windowSec). If the fan's stored window is notw, store[w, 0]. Then if the count is below the limit, add 1 and return true; otherwise return false without counting.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Keep one counter per fan, tagged with the window it belongs to. A request that falls in a new window resets the counter.
Trace the first example, with limit 3 and window 60:
- At t = 10 the request is in window 0. Ana has nothing stored, so start
[0, 0]. Since 0 < 3, the count becomes 1 and the request is allowed. - At t = 20 and t = 30 the requests are still in window 0. The counts become 2 and 3, and both are allowed.
- At t = 59 the request is in window 0 and the count of 3 is not below 3, so it is rejected and the count stays 3.
- At t = 60 the request is in window 1, which differs from the stored 0, so the record resets to
[1, 0]. The count becomes 1 and the request is allowed.
The weakness is that two windows meet at t = 60, so a fan can fit two full windows of requests into a few seconds. If that burst matters, use a sliding window, as in Password Reset Throttle.
class WindowLimiter {
constructor(limit, windowSec) {
this.limit = limit;
this.windowSec = windowSec;
this.counts = new Map(); // fan -> { window, count }
}
allow(fan, t) {
const w = Math.floor(t / this.windowSec);
let c = this.counts.get(fan);
if (!c || c.window !== w) {
c = { window: w, count: 0 }; // a new window starts from zero
this.counts.set(fan, c);
}
if (c.count >= this.limit) return false; // rejected: not counted
c.count++;
return true;
}
}class WindowLimiter:
def __init__(self, limit, window_sec):
self.limit = limit
self.window_sec = window_sec
self.counts = {} # fan -> [window, count]
def allow(self, fan, t):
w = t // self.window_sec
c = self.counts.get(fan)
if c is None or c[0] != w:
c = [w, 0] # a new window starts from zero
self.counts[fan] = c
if c[1] >= self.limit:
return False # rejected: not counted
c[1] += 1
return TrueRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.