Implement a Sliding Window Rate Limiter for an API Gateway

Last updated: November 29, 2026

Quick Overview

Implement allow(client_id, timestamp) that returns whether a request should be accepted, so that no client gets more than N accepted requests in any rolling window of W seconds.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Implement allow(client_id, timestamp) that returns whether a request should be accepted, so that no client gets more than N accepted requests in any rolling window of W seconds.

Microsoft teams that front public APIs (Azure API Management, Microsoft Graph throttling) like this question because it starts as a small data structure exercise and naturally opens into distributed design. The interviewer checks exact window semantics, memory per client, and whether you can name the trade-off between precision and memory.

What the Interviewer Expects
  • Pins down whether rejected requests count toward the limit and whether the window is inclusive
  • Implements an exact sliding log with amortized O(1) per call
  • Isolates state per client and reasons about memory growth
  • Compares sliding log, fixed window, sliding window counter, and token bucket
  • Sketches how to make it work across many gateway instances
Key Topics to Cover
Sliding window
Deque
Rate limiting
Token bucket
Distributed counters
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How do you cut memory to O(1) per client if a little imprecision is acceptable?
  • How would you share limits across 50 gateway nodes using Redis without a race between read and write?
  • How do you garbage-collect idle clients?
  • Graph returns Retry-After on 429: how would you compute it from your structure?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Each client may have at most limit accepted requests in any interval (now - window, now]. Rejected requests do not consume quota (that is how Graph throttling behaves, and it avoids punishing retr...

High-Level Design

The exact approach is a sliding log: keep the timestamps of accepted requests per client in a deque. On each call, pop timestamps older than the window from the left, then accept if fewer than limit...


Submit Your Answer
Markdown supported