Hit Counter
Implement a hit counter which keeps track of number of the number of hits in the last 60
seconds.
add(int timestamp)
which addstimestamp
in seconds in the hit countercount(int timestamp)
which returns the number of hits that have been made in the last60
seconds, given the current time istimestamp
.
You can assume that the timestamps passed into add
and count
are monotonically increasing.
Constraints
n ≤ 100,000
wheren
is the number of calls that are made toadd
andcount
https://binarysearch.com/problems/Hit-Counter
Examples
Example 1
Input
- methods =
['constructor', 'add', 'add', 'count', 'add', 'count']
- arguments =
[[], [10], [40], [40], [70], [100]]
Output
- answer =
[None, None, None, 2, None, 2]
Explanation
- We create a
HitCounter
- We add timestamp
10
to the data structure - We add timestamp
40
to the data structure - We count the number of timestamps that are within last
60
seconds of40
. There’s10
and40
so we return2
- We add timestamp
70
to the data structure - We count the number of timestamps that are within last
60
seconds of100
. There’s40
and70
so we return2
Leave a comment