DesignTheSystem/dev
MEDIUM Low-Level Design (LLD) 12 automated tests

Design Rate Limiter (Low-Level Design)

Two rate-limiting algorithms behind one interface, so callers can swap them. Focus: One interface, several algorithms.

DifficultyMEDIUM
Design focusOne interface, several algorithms
Automated grading3 public + 9 hidden tests (12 total)
Phase 2 follow-upPlans per client
LanguageTypeScript (OOP / Machine Coding)

Problem statement

Every limiter implements the same interface: allow(clientId, now) returns true if the request may go ahead. Time is in milliseconds.

TokenBucketLimiter(capacity, refillPerSecond): each client starts with a full bucket; each allowed request takes one token; tokens refill continuously and never exceed capacity.

SlidingWindowLimiter(limit, windowMs): a request is allowed if fewer than `limit` requests were allowed for that client in the last windowMs (requests at exactly now - windowMs are outside the window).

Clients never affect each other.

Functional requirements

  • interface RateLimiter { allow(clientId: string, now: number): boolean }
  • TokenBucketLimiter and SlidingWindowLimiter both implement RateLimiter.
  • Refused requests do not use up tokens or count toward the window.

Required API & starter interface

Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:

interface RateLimiter {
  allow(clientId: string, now: number): boolean;
}

class TokenBucketLimiter implements RateLimiter {
  constructor(capacity: number, refillPerSecond: number) {}

  allow(clientId: string, now: number): boolean {
    throw new Error("Not implemented");
  }
}

class SlidingWindowLimiter implements RateLimiter {
  constructor(limit: number, windowMs: number) {}

  allow(clientId: string, now: number): boolean {
    throw new Error("Not implemented");
  }
}

Public test cases

Your code runs against 3 public tests plus 9 hidden edge-case tests in a sandbox:

Token bucket allows a burst up to capacity (core)

var l = new TokenBucketLimiter(3, 1);
assertEqual([l.allow("a", 0), l.allow("a", 0), l.allow("a", 0), l.allow("a", 0)], [true, true, true, false]);

Sliding window counts the last window (core)

var l = new SlidingWindowLimiter(2, 1000);
assertEqual([l.allow("a", 0), l.allow("a", 500), l.allow("a", 900), l.allow("a", 1000)], [true, true, false, true]);

Clients are separate (core)

var l = new TokenBucketLimiter(1, 1);
assertEqual([l.allow("a", 0), l.allow("b", 0), l.allow("a", 0)], [true, true, false]);

Design & structure checks

Beyond passing tests, the grader inspects your class structure for clean object-oriented design:

  • Every limiter implements the RateLimiter interface: Callers depend on the interface, so you can swap algorithms or wrap them (as TieredLimiter does) without changing callers.
  • No switch on the algorithm name: Choosing behaviour with a type switch means editing that switch for every new algorithm. Separate classes keep each algorithm closed for change.

Phase 2: Change request

In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 12 tests, you unlock the change request: Plans per client.

Pair with High-Level System Design

Ready to scale Rate Limiter across distributed servers? Try the matching HLD simulator problem: Design Distributed Rate Limiter.

Relevant low-level design lessons

More low-level design problems