PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Read.Ai

Implement a per-user API rate limiter

Last updated: Mar 29, 2026

Quick Overview

This question evaluates the ability to implement per-user API rate limiting, testing competency in time-windowed request accounting, per-entity state management, and designing efficient data structures for time-based constraints.

  • medium
  • Read.Ai
  • Coding & Algorithms
  • Software Engineer

Implement a per-user API rate limiter

Company: Read.Ai

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are building an API gateway that must enforce per-user rate limits. ## Task Implement a rate limiter with the following API: - `RateLimiter(int maxRequests, int windowSeconds)` - `bool allow(string userId, int timestampSeconds)` `allow(...)` should return: - `true` if the request for `userId` at `timestampSeconds` is allowed - `false` if allowing it would exceed the limit ## Rate limit rule For each user, allow **at most `maxRequests` requests in any rolling window of `windowSeconds` seconds**. More formally, a request at time `t` is allowed iff the number of *previously allowed* requests with timestamps in `[t - windowSeconds + 1, t]` is strictly less than `maxRequests`. ## Notes / Assumptions - `timestampSeconds` is an integer in seconds. - Calls to `allow` may arrive in non-decreasing timestamp order (you may state/assume this; otherwise discuss how you would handle out-of-order input). - The solution should be efficient in time and memory for many users. ## Example If `maxRequests = 3`, `windowSeconds = 10` and a single user sends requests at times: - `1, 2, 3` → allowed - `4` → denied (would be 4 requests in window `[ -5, 4 ]` which effectively includes `1,2,3,4`) - `12` → allowed (window `[3,12]` contains `3` only among allowed requests)

Quick Answer: This question evaluates the ability to implement per-user API rate limiting, testing competency in time-windowed request accounting, per-entity state management, and designing efficient data structures for time-based constraints.

Read.Ai logo
Read.Ai
Nov 3, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
5
0

You are building an API gateway that must enforce per-user rate limits.

Task

Implement a rate limiter with the following API:

  • RateLimiter(int maxRequests, int windowSeconds)
  • bool allow(string userId, int timestampSeconds)

allow(...) should return:

  • true if the request for userId at timestampSeconds is allowed
  • false if allowing it would exceed the limit

Rate limit rule

For each user, allow at most maxRequests requests in any rolling window of windowSeconds seconds.

More formally, a request at time t is allowed iff the number of previously allowed requests with timestamps in [t - windowSeconds + 1, t] is strictly less than maxRequests.

Notes / Assumptions

  • timestampSeconds is an integer in seconds.
  • Calls to allow may arrive in non-decreasing timestamp order (you may state/assume this; otherwise discuss how you would handle out-of-order input).
  • The solution should be efficient in time and memory for many users.

Example

If maxRequests = 3, windowSeconds = 10 and a single user sends requests at times:

  • 1, 2, 3 → allowed
  • 4 → denied (would be 4 requests in window [ -5, 4 ] which effectively includes 1,2,3,4 )
  • 12 → allowed (window [3,12] contains 3 only among allowed requests)

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Read.Ai•More Software Engineer•Read.Ai Software Engineer•Read.Ai Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.