Design a hit counter which counts the number of hits received in the past 5 minutes (i.e., the past 300 seconds). Your system should accept a timestamp parameter (in seconds granularity), and you may assume that calls are being made to the system in chronological order (i.e., timestamp is monotonically non-decreasing). Several hits may arrive roughly at the same time. Implement the HitCounter class: - HitCounter() Initializes the object of the hit counter system. - void hit(int timestamp) Records a hit that happened at timestamp (in seconds). - int getHits(int timestamp) Returns the number of hits in the past 5 minutes from timestamp (i.e., the past 300 seconds).
Example 1
Input: operations = ["HitCounter","hit","hit","hit","getHits","hit","getHits","getHits"], arguments = [[],[1],[2],[3],[4],[300],[300],[301]]
Output: [null,null,null,null,3,null,4,3]
Explanation: Hits arrive at 1, 2 and 3, so getHits(4) sees 3. After a hit at 300, getHits(300) covers seconds 1-300 and sees 4. getHits(301) covers seconds 2-301, so the hit at time 1 has expired and the count is 3.
Example 2
Input: operations = ["HitCounter","getHits"], arguments = [[],[1]]
Output: [null,0]
Explanation: No hits have been recorded, so the count is 0.
Example 3
Input: operations = ["HitCounter","hit","getHits","getHits"], arguments = [[],[100],[100],[400]]
Output: [null,null,1,0]
Explanation: getHits(100) sees the single hit at time 100. By time 400 that hit is exactly 300 seconds old, which falls outside the window, so the count drops to 0.
Keep hits in a queue. On getHits, drop everything older than 300 seconds from the front; the queue size is the answer.
Run checks all cases above. Submit evaluates all test cases.