Design Rate Limiter (Low-Level Design)
Two rate-limiting algorithms behind one interface, so callers can swap them. Focus: One interface, several algorithms.
| Difficulty | MEDIUM |
|---|---|
| Design focus | One interface, several algorithms |
| Automated grading | 3 public + 9 hidden tests (12 total) |
| Phase 2 follow-up | Plans per client |
| Language | TypeScript (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
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
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
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
Strategy: swap behaviour, not code
Replace an if/else on a type with interchangeable strategy classes.
Open to extension, closed to edits
Shape a design so a new requirement means adding code, not rewriting it.
Systems that move step by step
Model a system that changes over time with a clear step rule.
More low-level design problems
Design LRU Cache
A fixed-size cache that throws out the least recently used entry when full, with every operation in constant time.
Design Game Leaderboard
Add points, show the top players and anyone's rank, with ties handled consistently.
Design Key-Value Store with Expiry
An in-memory store with per-key expiry, later with nested transactions.
Design Expense Sharing (Splitwise)
Split shared expenses several ways, track who owes whom, and suggest the fewest payments to settle up.