All problems
Design Hit Counter
Problem
Simulate a hit counter that counts hits in the last 300 seconds.
operations contains "hit" (records a hit at timestamps[i]) or "get_hits" (timestamps[i] is the query time). A hit is counted when its timestamp lies in [t - 299, t].
Return the list of recorded results (None for hit, an integer for get_hits).
Hint
The window is inclusive on both ends: cutoff = query time - 299.
Test cases
case 1
in: [["hit","hit","hit","get_hits","get_hits"],[1,2,3,4,300]]
expected: [null,null,null,3,3]
case 2
in: [["hit","get_hits"],[1,301]]
expected: [null,0]
case 3
in: [["get_hits"],[10]]
expected: [0]
case 4
in: [["hit","hit","hit","hit","get_hits"],[10,10,10,400,401]]
expected: [null,null,null,null,1]
Starter codePython
def hit_counter(operations, timestamps):
# Your code here
pass4 test cases · run your code against them in the editor