Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sliding Window Rate Limiter

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Sliding Window Rate Limiter. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

At Stripe, you need an in-memory rate limiter that decides whether each request should be allowed.

Implement rate_limit(timestamps, limit, window_seconds) that returns a boolean decision for every request timestamp.

Specification

  • Input:
    • timestamps: list[int] (non-decreasing)
    • limit: int maximum number of allowed requests in any window
    • window_seconds: int window size in seconds
  • Window rule: for a request at time t, consider the inclusive window [t - window_seconds, t].
  • Counting rule: only allowed requests count toward the limit.
  • Output: list[bool] where True means allow, False means deny.

Constraints

  • 0 <= len(timestamps) <= 2 * 10^5
  • 0 <= timestamps[i] <= 10^9
  • 1 <= limit <= 10^5
  • 1 <= window_seconds <= 10^9
  • timestamps is non-decreasing

Function Signature

def rate_limit(timestamps: list[int], limit: int, window_seconds: int) -> list[bool]:
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output