Description

Design a hit counter which counts the number of hits received in the past 5 minutes (300 seconds). Implement hit(timestamp) and getHits(timestamp) methods.

Examples

Input:hit(1), hit(2), hit(3), getHits(4), hit(300), getHits(300), getHits(301)
Output:[3,4,3]
Explanation:

Hits within 300s window.

Input:hit(100), hit(150), getHits(400), hit(450), getHits(500), getHits(750)
Output:[2, 2, 1]
Explanation:

At timestamp 400, both hits at 100 and 150 are within the 300-second window (400-300=100). At timestamp 500, the same two hits are still valid. At timestamp 750, only the hit at 450 remains valid since hits at 100 and 150 are now outside the window (750-300=450).

Input:getHits(50), hit(200), hit(250), hit(499), getHits(500), hit(600), getHits(900)
Output:[0, 3, 1]
Explanation:

At timestamp 50, no hits have been recorded yet. At timestamp 500, all three hits (200, 250, 499) are within the 300-second window (500-300=200). At timestamp 900, only the hit at 600 remains valid since the earlier hits fall outside the window (900-300=600).

Constraints

  • 1 ≤ timestamp ≤ 2 × 10⁹
  • Calls made in chronological order of timestamp.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!