Skip to main content

Design Hit Counter

Medium
Data Structures and Algorithms
Editorial
Topics:
Array
Binary Search
Design

You are given a class HitCounter that records hits at given timestamps (in seconds). Implement the following methods:

  • hit(timestamp): Records a hit that happened at the given timestamp (an integer representing the current time in seconds).
  • getHits(timestamp): Returns the number of hits recorded in the past 5 minutes (i.e., in the time interval [timestamp - 299, timestamp], inclusive).

Both methods will be called with timestamps in chronological order (i.e., the timestamp parameter is always greater than or equal to the previous calls).

Return the number of hits in the past 5 minutes for each call to getHits.

Need help or want more practice?

Practise in an interviewGet AI Coach help