Implement a Thread-Safe Per-Client Sliding-Window Rate Limiter

Quick Overview

Implement a thread-safe sliding-window rate limiter that tracks each client separately, allows or denies each request, records only allowed requests and prunes expired timestamps. A follow-up part extends the limiter into a shared service in front of many API servers.

Implement a Thread-Safe Per-Client Sliding-Window Rate Limiter

Company: StackAdapt

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

Implement a sliding-window rate limiter that restricts how many requests a client can make within a rolling time window. The requirements as given: - Per-client tracking: each client has an independent request count. - Sliding time window: only requests within the configured window count toward the limit. - Allow or deny: return whether a request is allowed, and record it when it is allowed. - Handle concurrent requests (thread-safe). - Be memory-efficient: prune expired timestamps. The phone screen then moved to system design questions about the same limiter. The source does not list them, so Part 2 covers the most direct one: enforcing the limit for a fleet of servers. ### Constraints and Clarifications - The limit (N requests) and the window length (W seconds) are fixed when the limiter is constructed. - A denied request is not recorded, so it does not count against the client. - Expose something like `allow(client_id) -> bool`, and take the clock as a constructor argument so tests can control time. ### Clarifying Questions - Is the limit the same for every client, or configured per client or tier? - Does a request exactly W seconds old still count? - Roughly how many distinct clients and requests per second must one process handle, and how large can N be? - Is an exact count required, or is a close approximation acceptable if it saves memory? - Should the caller learn when a denied client may retry? ### Part 1: Implement the limiter Write the class: the data structure kept per client, the allow or deny decision, the pruning of expired entries, and the locking that keeps concurrent calls correct. ```hint Order of timestamps A client's allowed requests are recorded in time order. Use that ordering to make dropping expired entries cheap, and check whether your locking keeps the order true when two threads call at once. ``` ```hint What must be atomic Pruning, counting and recording form one decision. Consider what goes wrong if another thread runs between the count and the record, and how wide the lock needs to be. ``` #### What This Part Should Cover - Correct window semantics: prune, compare with the limit, record only on allow. - Thread safety: one atomic check-and-record per client, and lock granularity across clients. - Memory: at most N timestamps per client, and removal of idle clients. - Time and space complexity per call. ### Part 2: One limit across many servers The limiter now protects an API served by many stateless servers behind a load balancer, and one client's requests can reach any of them. How do you enforce the same per-client limit, and what happens when the shared piece is slow or unavailable? #### Clarifying Questions for this Part - How strict must the limit be across servers: is a small overshoot during bursts acceptable? - When the limiter's state store is down, should requests be allowed or denied? ```hint Where the count lives Each server alone sees only part of a client's traffic. Decide where the shared state lives and what has to happen in one atomic step on every request. ``` #### What This Part Should Cover - Shared state, and an atomic check-and-record against it. - The latency added per request and how to reduce it. - Behavior when the store is slow or down. - Memory of an exact log versus approximate counters at fleet scale. ### What a Strong Answer Covers - A tested implementation with an injectable clock. - Edge cases: bursts at the same timestamp, a request exactly W old, a limit of 0, many idle clients. - Amortized and worst-case cost per call. - The trade-off between an exact sliding log and an approximate sliding-window counter, carried from one process to the fleet. ### Follow-up Questions - How would you return a retry-after time with each denied request? - How would you support different limits per customer tier without slowing every lookup? - How would you test that the limiter stays correct under concurrent calls? - A client floods the endpoint far above its limit. What does each denied call cost, and can the deny path be made cheaper?

Overview: Implement a thread-safe sliding-window rate limiter that tracks each client separately, allows or denies each request, records only allowed requests and prunes expired timestamps. A follow-up part extends the limiter into a shared service in front of many API servers.

Read the full StackAdapt Software Engineer interview experience this question came from

StackAdapt logo
StackAdapt
Nov 24, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
0
0

Implement a sliding-window rate limiter that restricts how many requests a client can make within a rolling time window. The requirements as given:

  • Per-client tracking: each client has an independent request count.
  • Sliding time window: only requests within the configured window count toward the limit.
  • Allow or deny: return whether a request is allowed, and record it when it is allowed.
  • Handle concurrent requests (thread-safe).
  • Be memory-efficient: prune expired timestamps.

The phone screen then moved to system design questions about the same limiter. The source does not list them, so Part 2 covers the most direct one: enforcing the limit for a fleet of servers.

Constraints and Clarifications

  • The limit (N requests) and the window length (W seconds) are fixed when the limiter is constructed.
  • A denied request is not recorded, so it does not count against the client.
  • Expose something like allow(client_id) -> bool , and take the clock as a constructor argument so tests can control time.

Clarifying Questions Guidance

  • Is the limit the same for every client, or configured per client or tier?
  • Does a request exactly W seconds old still count?
  • Roughly how many distinct clients and requests per second must one process handle, and how large can N be?
  • Is an exact count required, or is a close approximation acceptable if it saves memory?
  • Should the caller learn when a denied client may retry?

Part 1: Implement the limiter

Write the class: the data structure kept per client, the allow or deny decision, the pruning of expired entries, and the locking that keeps concurrent calls correct.

What This Part Should Cover Guidance

  • Correct window semantics: prune, compare with the limit, record only on allow.
  • Thread safety: one atomic check-and-record per client, and lock granularity across clients.
  • Memory: at most N timestamps per client, and removal of idle clients.
  • Time and space complexity per call.

Part 2: One limit across many servers

The limiter now protects an API served by many stateless servers behind a load balancer, and one client's requests can reach any of them. How do you enforce the same per-client limit, and what happens when the shared piece is slow or unavailable?

Clarifying Questions for this Part Guidance

  • How strict must the limit be across servers: is a small overshoot during bursts acceptable?
  • When the limiter's state store is down, should requests be allowed or denied?

What This Part Should Cover Guidance

  • Shared state, and an atomic check-and-record against it.
  • The latency added per request and how to reduce it.
  • Behavior when the store is slow or down.
  • Memory of an exact log versus approximate counters at fleet scale.

What a Strong Answer Covers Guidance

  • A tested implementation with an injectable clock.
  • Edge cases: bursts at the same timestamp, a request exactly W old, a limit of 0, many idle clients.
  • Amortized and worst-case cost per call.
  • The trade-off between an exact sliding log and an approximate sliding-window counter, carried from one process to the fleet.

Follow-up Questions Guidance

  • How would you return a retry-after time with each denied request?
  • How would you support different limits per customer tier without slowing every lookup?
  • How would you test that the limiter stays correct under concurrent calls?
  • A client floods the endpoint far above its limit. What does each denied call cost, and can the deny path be made cheaper?