Proof of Work · Challenge
Allow a burst, then a steady trickle.
about 25 min · 7 required tests · in the browser
What is recorded: when you save, your Result page shows your code, which tests passed, how many times you ran the tests, how many versions you tried, the time from first run to save, and the largest jump in code between runs. Anyone with the link can see it. The suggested time is not enforced.
An API should let a client send a quick burst of requests, then slow them to a steady rate. A token bucket does that: the bucket holds tokens, each request spends some, and tokens drip back in over time.
Implement createLimiter({ capacity, refillPerSecond }). It returns an object with one method, tryRemove(n).
capacity tokens.tryRemove(n) returns true and spends n tokens if the bucket holds at least n. Otherwise it returns false and spends nothing.n defaults to 1.refillPerSecond, in fractions. Half a second at 2 per second adds 1 token. The bucket never holds more than capacity.Capacity 3, refilling 1 token per second:
const limiter = createLimiter({ capacity: 3, refillPerSecond: 1 })
limiter.tryRemove(2) // true, 1 token left
limiter.tryRemove(2) // false, nothing spent
// 1 second passes, 2 tokens in the bucket
limiter.tryRemove(2) // trueThe grader moves time on a virtual clock. Date.now works. Time does not pass on its own, and setInterval is not available. Read the time when tryRemove is called instead of running a timer.
Requirements:
capacity.capacity.capacity is always refused.Write a top-level function named createLimiter. Do not use export or module.exports.
Press Esc, then Tab, to leave the editor.
⌘ / Ctrl + Enter runs the tests
Hiring developers? See how teams use this.
Graded on
0/7
Other builds