Implement a sliding-window rate limiter with per-client remaining-request counts

Quick Overview

Build a per-client rate limiter with the sliding-window log algorithm, with one method that accepts or rejects a request and another that reports how many requests remain in the current window. It tests queue-based eviction, window boundary handling, thread safety and amortized complexity.

Implement a sliding-window rate limiter with per-client remaining-request counts

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a rate limiter that follows the *sliding-window* algorithm. Implement a class `SlidingWindow` configured with two values: - `maxRequests`: the maximum number of requests a single client may make in any contiguous window of length `windowSeconds`. - `windowSeconds`: the length, in seconds, of the sliding time window checked for each request. Each client, identified by `clientId`, is tracked independently. The methods read the current time from the clock; call it `now`. A request is allowed if, after discarding the client's timestamps that are older than `now - windowSeconds`, the client has made strictly fewer than `maxRequests` requests in the remaining window. Implement these methods: - `SlidingWindow(max_requests: number, window_seconds: number)`: initialize the data structure. Both arguments are optional. - `allowRequest(client: string) -> boolean`: if the request is allowed, record it and return `true`. Otherwise return `false` and record nothing. - `getRemainingRequests(client: string) -> int`: return the number of additional requests the client may still make in the current window, as a non-negative integer. Example with `SlidingWindow(3, 10)`, where `t` is the time in seconds since the limiter was created: ```text t = 0 allowRequest("a") -> true t = 1 allowRequest("a") -> true t = 2 allowRequest("a") -> true t = 3 allowRequest("a") -> false t = 3 getRemainingRequests("a") -> 0 t = 3 allowRequest("b") -> true t = 3 getRemainingRequests("c") -> 3 t = 10 getRemainingRequests("a") -> 0 t = 11 getRemainingRequests("a") -> 1 t = 11 allowRequest("a") -> true t = 13 getRemainingRequests("a") -> 2 ``` The rejected request at t = 3 is not recorded, and client "b" has its own window. A client that has never made a request has all 3 requests available. At t = 10 the cutoff is 0, and the request made at 0 is not older than the cutoff, so it still counts. At t = 11 the cutoff is 1, so only the request at 0 drops out. At t = 13 the requests at 1 and 2 have expired, leaving only the one made at 11. ```hint Where the expired requests are Requests are recorded in time order, so for each client the expired timestamps are always the oldest ones. Pick a per-client structure that makes dropping them cheap. ``` ```hint Watch the boundary Compare your eviction condition with the rule: a timestamp exactly windowSeconds old is not older than the cutoff. Then think about how a test could control the clock to check that case. ``` ### Constraints & Assumptions - `1 <= maxRequests <= 1000` - `1 <= windowSeconds <= 1000` - Up to 100,000 total calls to `allowRequest` and `getRemainingRequests`. - `clientId` is a non-empty ASCII string of at most 64 characters. - Calls arrive in real time and in order. Two successive calls can happen at the same instant (they are at least 0 ms apart). ### Clarifying Questions to Ask - What defaults should the constructor use when an argument is omitted? - Can the limiter be called from several threads at once? - Does one in-memory instance enforce the limit, or must several server processes share it? - Is memory for clients that stop sending requests a concern, or is the number of distinct clients small? ### What a Strong Answer Covers - A per-client log of accepted request timestamps with expired entries dropped from the oldest end, and why rejected requests are not recorded. - A window boundary that matches the rule (a timestamp exactly `windowSeconds` old still counts) and correct results for a client never seen before. - The amortized and worst-case cost per call, and memory per client bounded by `maxRequests`. - Thread safety, including where the clock is read relative to the lock. - A monotonic clock that a test can replace, so the behavior can be checked deterministically. ### Follow-up Questions - How would you enforce one limit per client across many API servers? - The log stores up to `maxRequests` timestamps per client. How would you approximate the sliding window with constant memory per client, and what error does that introduce? - How would you reclaim memory for clients that stop sending requests? - How would you tell a rejected client when it can retry?

Overview: Build a per-client rate limiter with the sliding-window log algorithm, with one method that accepts or rejects a request and another that reports how many requests remain in the current window. It tests queue-based eviction, window boundary handling, thread safety and amortized complexity.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a rate limiter that follows the sliding-window algorithm.

Implement a class SlidingWindow configured with two values:

  • maxRequests : the maximum number of requests a single client may make in any contiguous window of length windowSeconds .
  • windowSeconds : the length, in seconds, of the sliding time window checked for each request.

Each client, identified by clientId, is tracked independently. The methods read the current time from the clock; call it now. A request is allowed if, after discarding the client's timestamps that are older than now - windowSeconds, the client has made strictly fewer than maxRequests requests in the remaining window.

Implement these methods:

  • SlidingWindow(max_requests: number, window_seconds: number) : initialize the data structure. Both arguments are optional.
  • allowRequest(client: string) -> boolean : if the request is allowed, record it and return true . Otherwise return false and record nothing.
  • getRemainingRequests(client: string) -> int : return the number of additional requests the client may still make in the current window, as a non-negative integer.

Example with SlidingWindow(3, 10), where t is the time in seconds since the limiter was created:

t = 0    allowRequest("a")            -> true
t = 1    allowRequest("a")            -> true
t = 2    allowRequest("a")            -> true
t = 3    allowRequest("a")            -> false
t = 3    getRemainingRequests("a")    -> 0
t = 3    allowRequest("b")            -> true
t = 3    getRemainingRequests("c")    -> 3
t = 10   getRemainingRequests("a")    -> 0
t = 11   getRemainingRequests("a")    -> 1
t = 11   allowRequest("a")            -> true
t = 13   getRemainingRequests("a")    -> 2

The rejected request at t = 3 is not recorded, and client "b" has its own window. A client that has never made a request has all 3 requests available. At t = 10 the cutoff is 0, and the request made at 0 is not older than the cutoff, so it still counts. At t = 11 the cutoff is 1, so only the request at 0 drops out. At t = 13 the requests at 1 and 2 have expired, leaving only the one made at 11.

Constraints & Assumptions

  • 1 <= maxRequests <= 1000
  • 1 <= windowSeconds <= 1000
  • Up to 100,000 total calls to allowRequest and getRemainingRequests .
  • clientId is a non-empty ASCII string of at most 64 characters.
  • Calls arrive in real time and in order. Two successive calls can happen at the same instant (they are at least 0 ms apart).

Clarifying Questions to Ask Guidance

  • What defaults should the constructor use when an argument is omitted?
  • Can the limiter be called from several threads at once?
  • Does one in-memory instance enforce the limit, or must several server processes share it?
  • Is memory for clients that stop sending requests a concern, or is the number of distinct clients small?

What a Strong Answer Covers Guidance

  • A per-client log of accepted request timestamps with expired entries dropped from the oldest end, and why rejected requests are not recorded.
  • A window boundary that matches the rule (a timestamp exactly windowSeconds old still counts) and correct results for a client never seen before.
  • The amortized and worst-case cost per call, and memory per client bounded by maxRequests .
  • Thread safety, including where the clock is read relative to the lock.
  • A monotonic clock that a test can replace, so the behavior can be checked deterministically.

Follow-up Questions Guidance

  • How would you enforce one limit per client across many API servers?
  • The log stores up to maxRequests timestamps per client. How would you approximate the sliding window with constant memory per client, and what error does that introduce?
  • How would you reclaim memory for clients that stop sending requests?
  • How would you tell a rejected client when it can retry?