Hit counter sliding window
Your task
Design a hit counter that records hits (each carrying a timestamp in seconds) and can report how many hits occurred in the most recent 300 seconds (a 5-minute window). Implement it with a fixed-size circular buffer of 300 slots, where slot i holds (last_timestamp_seen_in_that_slot, hit_count). Process a list of operations: ['hit', t] records a hit at timestamp t and returns -1 (no output value), and ['getHits', t] returns the number of hits in the window (t-299 .. t] inclusive. Timestamps are positive and non-decreasing across operations. Return the list of results for getHits operations only, in order. There are 1 to 10^4 operations.
Examples
run_hit_counter(ops: list[list]) -> list[int]Example 1
Input
run_hit_counter([["hit", 1], ["hit", 2], ["hit", 3], ["getHits", 4], ["hit", 300], ["getHits", 300], ["getHits", 301]])Output
[3, 4, 3]