Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    A per-client rate limiter

    MediumAbout 25 minFull AccessLearn it: Output Caching, HybridCache and Rate Limiting: Three Tools, Three Jobs

    The task

    A public API lets each client make only a few requests per time window, so that one busy client cannot slow everyone down.

    Complete Solution.CreateRateLimiter(int requestLimit, int windowSeconds), which returns a new Solution.RateLimiter, and its method AllowRequest(string client, int timeSeconds), which returns true when the request is allowed and false when it is rejected. The rules:

    • Windows are fixed and start at time zero: with a window of 10 seconds they are [0, 10), [10, 20) and so on. The start belongs to the window; the end belongs to the next one.
    • Each client may have at most requestLimit allowed requests in each window. Clients have separate counts.
    • Once a client reaches the limit, its other requests in that window are rejected. Each new window gives that client its full allowance again.
    • Each limiter returned by CreateRateLimiter has its own state.

    The limit and the window are positive, client IDs are non-null strings, and times are non-negative whole seconds. Calls on a limiter happen one at a time, in non-decreasing time order; several requests may share a time. Use the time you are given, not a clock.

    Signature

    public static RateLimiter CreateRateLimiter(int requestLimit, int windowSeconds)
    public sealed class RateLimiter
    public bool AllowRequest(string client, int timeSeconds)

    Examples

    • limit 2, window 10, then AllowRequest("a", 1), ("a", 2), ("a", 9), ("a", 10) → true, true, false, true. The third request in [0, 10) is over the limit, and 10 starts a new window.
    • limit 1, window 10, then AllowRequest("a", 4), ("b", 4) → true, true. Each client has its own count.

    Limits: Up to 100,000 calls on one limiter, from up to 10,000 clients.

    The tests check: a first request at time zero, a full allowance, rejected requests, separate clients, the exact window boundary, windows that start at zero (not at a client's first request), a fresh allowance after idle windows, requests at the same time, independent limiters, a one-second window, and 100,000 calls from 10,000 clients in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // Returns a new limiter with its own state.
          public static RateLimiter CreateRateLimiter(int requestLimit, int windowSeconds)
          {
              throw new NotImplementedException();
          }
      
          public sealed class RateLimiter
          {
              internal RateLimiter(int requestLimit, int windowSeconds)
              {
              }
      
              // Returns whether this client's request is allowed at the given time.
              public bool AllowRequest(string client, int timeSeconds)
              {
                  throw new NotImplementedException();
              }
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.