Password Reset Throttle
A bank's "forgot password" button emails a reset link. Attackers use it to flood inboxes, so each address gets at most 3 reset emails in any rolling hour. Unlike a per-clock-hour counter, that forbids 3 emails at 9:59 plus 3 more at 10:00. When the button is blocked, the page says how long to wait.
This is the sliding-window log from Rate Limiting: it stores the time of every allowed request in the window, costing more memory than a counter but exact. It suits low limits on sensitive actions such as logins, sign-ups and reset emails, and the wait time becomes the Retry-After header of an HTTP 429 response.
Your task
Write a class ResetThrottle:
constructor(limit, windowSec)sets the limit and the window length in seconds.request(email, t): a reset request at timet(whole seconds). Returntrueand record it if the address has had fewer thanlimitallowed requests in the window; otherwise returnfalseand record nothing.waitTime(email, t): return how many seconds fromtuntil a request for this address would be allowed. Return 0 if one would be allowed att. This does not record a request.
The window at time t covers the times greater than t − windowSec and up to t. So a request made at time s stops counting at exactly s + windowSec. Each address is limited separately.
class ResetThrottle
Examples
new ResetThrottle(3, 3600)
.request("kai@mail.test", 0)
.request("kai@mail.test", 600)
.request("kai@mail.test", 1200)
.request("kai@mail.test", 1800)
.waitTime("kai@mail.test", 1800)
.request("kai@mail.test", 3599)
.request("kai@mail.test", 3600)
.waitTime("kai@mail.test", 3600)[null, true, true, true, false, 1800, false, true, 600]
Requests at 0, 600 and 1,200 fill the limit of 3. At 1,800 the request is refused, and the request at 0 stops counting at 3,600, so the wait is 1,800 seconds. At 3,599 the request at 0 still counts, so it is refused. At 3,600 the request at 0 has left the window, so the new request is allowed. The window now holds 600, 1,200 and 3,600, and the next slot opens when 600 leaves at 4,200, so the wait at 3,600 is 600 seconds.
new ResetThrottle(2, 60)
.request("mo@mail.test", 58)
.request("mo@mail.test", 59)
.request("mo@mail.test", 60)
.request("mo@mail.test", 61)
.request("li@mail.test", 61)[null, true, true, false, false, true]
A fixed per-minute counter would reset at 60 and allow 4 requests between 58 and 61. Here the window at 60 still holds 58 and 59, so 60 and 61 are refused. Another address has its own log.
Constraints
- 1 ≤ limit ≤ 1,000
- 1 ≤ windowSec ≤ 1,000,000
- 0 ≤ t ≤ 1,000,000,000, and t never decreases from one call to the next
- email is a non-empty string of up to 40 characters
- At most 3,000 calls per test
Start with brute force
Store every allowed request forever and count the stored times inside the window on each call. With 3,000 calls that is up to about 4.5 million comparisons, and memory grows with the whole history.
Expired times never count again, so dropping them from the front of a queue keeps at most limit times per address and makes each call O(1) on average.
Hints
0 of 4 shown- Hint 1
At time
t, which past requests still matter? Once a request is too old for the window, can it ever matter again, given that time only moves forward? - Hint 2
Old requests leave the window in the order they were made. That is a queue: new times join at the back, expired times leave from the front.
- Hint 3
Keep a queue of allowed request times per address. On every call, first drop times
sfrom the front whiles ≤ t − windowSec. The queue length is then the count in the window. - Hint 4
A request is only recorded when there is room, so the queue never holds more than
limittimes. When it is full, the front slot frees up first: the wait isfront + windowSec − t.
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 queue of allowed request times per address. Each call first drops the times that have left the window; the queue length is then the count inside it.
Trace the first example, limit 3, window 3,600:
- The requests at 0, 600 and 1,200 are all allowed, and the queue becomes
[0, 600, 1200]. - At 1,800 no time is ≤ 1,800 − 3,600, so nothing leaves the queue. The window holds 3 requests, so the request is refused, and
waitTimeis 0 + 3,600 − 1,800 = 1,800. - At 3,599 the time 0 is not ≤ −1, so it stays in the queue. The queue is still full and the request is refused.
- At 3,600 the time 0 is ≤ 0, so it leaves the queue. Two requests remain in the window, so the new request is allowed and the queue becomes
[600, 1200, 3600]. - The queue is full again at 3,600 and its front is 600, so
waitTimeis 600 + 3,600 − 3,600 = 600.
The exact log costs memory, because it stores up to limit timestamps per address where a counter stores one number. The log therefore suits small limits such as 3 an hour, while a token bucket or a sliding-window counter suits limits such as 1,000 a minute.
class ResetThrottle {
constructor(limit, windowSec) {
this.limit = limit;
this.windowSec = windowSec;
this.logs = new Map(); // email -> allowed request times, oldest first
}
window(email, t) {
if (!this.logs.has(email)) this.logs.set(email, []);
const log = this.logs.get(email);
// drop times that are no longer inside (t - windowSec, t]
while (log.length && log[0] <= t - this.windowSec) log.shift();
return log;
}
request(email, t) {
const log = this.window(email, t);
if (log.length >= this.limit) return false; // full: not recorded
log.push(t);
return true;
}
waitTime(email, t) {
const log = this.window(email, t);
if (log.length < this.limit) return 0;
return log[0] + this.windowSec - t; // the oldest leaves first
}
}from collections import deque
class ResetThrottle:
def __init__(self, limit, window_sec):
self.limit = limit
self.window_sec = window_sec
self.logs = {} # email -> deque of allowed request times, oldest first
def _window(self, email, t):
log = self.logs.setdefault(email, deque())
# drop times that are no longer inside (t - window_sec, t]
while log and log[0] <= t - self.window_sec:
log.popleft()
return log
def request(self, email, t):
log = self._window(email, t)
if len(log) >= self.limit: # full: not recorded
return False
log.append(t)
return True
def wait_time(self, email, t):
log = self._window(email, t)
if len(log) < self.limit:
return 0
return log[0] + self.window_sec - t # the oldest leaves firstshift moves the rest of the array, but the queue never holds more than limit times; a deque avoids even that.)Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.