⌘K
Flycodx

API Rate Limiter

API Rate Limiter

HardBackend11% accepted

Fixed-window rate limiter. Requests arrive at integer timestamps. The window for timestamp t is floor(t / W). Allow a request if its window has seen at most K requests so far (including itself), otherwise BLOCK. Return the decision (ALLOW/BLOCK) for each request in order.

Approach

Given a stream of request timestamps and a fixed-window rate limit, output ALLOW or BLOCK for each request in order. At most k requests are allowed within each fixed window of w seconds, where a timestamp t belongs to window (t // w) — windows are aligned to multiples of w (NOT a sliding window). Args: solve(nums, k, w).

Input

6 3 5 1 2 3 4 5 6

Output

["ALLOW","ALLOW","ALLOW","BLOCK","ALLOW","ALLOW"]
solution.py
Loading editor...
Test 1
Test 2
Test 3

Press Run to test your custom input, or Submit to judge against all test cases.

Sign in to continue

Create a free account to run code, submit solutions and track progress.

  • Free forever — no credit card
  • 10+ languages with live execution
  • Points, streaks & leaderboards
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3