Home
← All questions

Token bucket rate limiter in JavaScript

HardPremiumJavaScript

Asked at Stripe · Google · Amazon · Uber

JavaScript interview practice: Implement a token-bucket rate limiter used in API gateways and client-side throttling.

Problem

Implement a token bucket rate limiter:

  • The bucket holds at most capacity tokens.
  • Tokens refill at refillRate tokens per second (continuous refill).
  • allowRequest(tokens = 1) returns true if enough tokens are available and deducts them; otherwise false.

Used to protect APIs from abuse while allowing short bursts.

Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium