Count events over a sliding window: a logger rate limiter, a hit counter and a moving average

mediumAbout 35 minutesUpdated CodingProsliding-windowqueuesdesignrate-limiting
Not solved

Share this question

Problem

Analytics, logging and rate limiting all ask the same question: what happened in the last few seconds? Build three small classes. Times are integer seconds and arrive in non-decreasing order for each instance.

const logger = new Logger();
logger.shouldPrint(1, "foo");  // true
logger.shouldPrint(2, "bar");  // true
logger.shouldPrint(3, "foo");  // false (printed at 1)
logger.shouldPrint(11, "foo"); // true  (10 seconds after 1)

Ref. UR-1423 · © UIRound · uiround.com/questions/count-events-over-a-sliding-window-a-logger-rate-limiter-a-hit-counter-and-a-moving-average

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment