Hit counter
Design a hit counter that counts hits in the most recent 300 seconds (a 5-minute sliding window). Process ops: ['hit', timestamp] records a hit at the given second (timestamps are non-decreasing), and ['getHits', timestamp] returns the number of hits in the window (timestamp-299 .. timestamp] inclusive, i.e. strictly more recent than timestamp-300. Return the list of getHits results.
Implement
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 20 min
solution.py
InputExpectedGot
[[["hit",1],["hit",2],["hit",3],["getHits",4],["hit",300],["getHits",300],["getHits",301]]][3,4,3]not run yetsample