Presale Search Cap

EasydesignAlso known as Fixed Window Rate Limiter

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 most limit allowed requests per window.
  • allow(fan, t) handles a request from fan at time t, in whole seconds. It returns true if the request is allowed and false if 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

Example 1
Input
new WindowLimiter(3, 60)
.allow("ana", 10)
.allow("ana", 20)
.allow("ana", 30)
.allow("ana", 59)
.allow("ana", 60)
Output
[null, true, true, true, false, true]
Why

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.

Example 2
Input
new WindowLimiter(2, 60)
.allow("ben", 58)
.allow("ben", 59)
.allow("cy", 59)
.allow("ben", 60)
.allow("ben", 61)
Output
[null, true, true, true, true, true]
Why

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

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.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code