Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Design hit counter and log storage system



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Problem Statement - Build a Recent Hit Tracker

Build a Recent Hit Tracker

Problem Statement

Create a tracker that records the number of hits received within the last five minutes.

Each method receives a timestamp (in seconds) as input, and you can assume the timestamps are provided in increasing order (i.e., the timestamp values never decrease). The earliest timestamp will always start at 1.

Multiple hits may occur at the same timestamp.

Examples

HitTracker tracker = new HitTracker();

// record a hit at second 1
tracker.recordHit(1);

// record a hit at second 2
tracker.recordHit(2);

// record a hit at second 4
tracker.recordHit(4);

// get the number of hits at second 5, should return 3
tracker.getRecentHits(5);

// record a hit at second 301
tracker.recordHit(301);

// get the number of hits at second 301, should return 4
tracker.getRecentHits(301);

// get the number of hits at second 302, should return 3
tracker.getRecentHits(302);
    

Constraints

  • 1 <= timestamp <= 1000000
  • Calls to recordHit and getRecentHits are made in non-decreasing order of timestamp
  • Number of hits per second can be large, but your solution should efficiently handle them