Design Hit Counter

Medium · Design · Design · Queue · Sliding Window

Problem

Count how many hits arrived in the past five minutes (300 seconds).

Timestamps arrive in non-decreasing order, and several hits may share a second.

Your tests replay a call sequence: the first entry names the class, each later entry is a method with its arguments, and calls returning nothing report None.

Examples

Input: hit(1), hit(2), hit(3), getHits(4), hit(300), getHits(300), getHits(301)
Output: 3, then 4, then 3

At t=301 the window is (1, 301], so the hit at second 1 has just fallen out.

Constraints

Approaches

Follow-up

What if hits arrive at a million per second, or timestamps come as fractions of a second? A queue grows with traffic; per-second buckets do not, but they need an integer second to index by.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…