Design Hit Counter
Problem: Design Hit Counter
Code in Python:
class HitCounter(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.hits = []
def hit(self, timestamp):
"""
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
:type timestamp: int
:rtype: void
"""
self.hits = [timestamp] + self.hits
def getHits(self, timestamp):
"""
Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity).
:type timestamp: int
:rtype: int
"""
count = 0
for hit in self.hits:
if hit > timestamp - 300: count += 1
return count
# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)