Code RoomHit counter sliding window
MediumPrep Room Coding #311

Hit counter sliding window

CodingNetworking & APIsMid–Senior~25 min

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.

Implement
run_hit_counter(ops: list[list]) → list[int]
Examples
in[[["hit",1],["hit",2],["hit",3],["getHits",4],["hit",300],["getHits",300],["getHits",301]]]out[3,4,3]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[[["hit",1],["hit",2],["hit",3],["getHits",4],["hit",300],["getHits",300],["getHits",301]]][3,4,3]not run yetsample