Codetive
Sign In
Coding challenges as of now are only available on desktop
On mobile you can try our quiz
Quiz
We are working hard to bring coding challenges with editor on mobile 💚

Rate limiter

Objective: Implement a rate limiter that restricts the number of function executions within a specified time window.

Instructions:

  1. Create a function named rateLimiter that accepts two arguments:
    • A function to limit.
    • The maximum number of times the function can be executed within a specified time window (e.g., 5 times per 10 seconds).
  2. Ensure that after reaching the limit, the function is blocked from executing until the time window resets.

Example Usage:

const limitedFunction = rateLimiter(apiCall, 5, 10000); // 5 calls per 10 seconds
 
limitedFunction(); // Executes the first call
limitedFunction(); // Executes the second call
// After 5 calls in 10 seconds, the function will be blocked until the next window starts.

Requirements:

  • The rateLimiter should limit how many times the function can be executed within a specific time window.
  • Once the limit is reached, further function calls should be blocked until the time window resets.
  • The time window should reset after a specified duration (e.g., every 10 seconds).

Hints:

  1. Use timestamps to track when the time window starts and the number of function calls.
  2. Use a counter to track how many times the function has been executed within the time window.

Bonus:

  1. Add the ability to reset the limiter manually.
  2. Add key for each action, so different actions can be rate limited separately
Browser
Console
Tests
Soon