Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Hit Counting and Time Windows

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Hit Counting and Time Windows. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Vercel may need to report how many requests a deployment received during a specified time window. Maintain hit timestamps for a Vercel deployment and support efficient range-count queries.

Implement two functions:

  1. record_hit(hit_times, timestamp): Add one hit timestamp while keeping hit_times sorted.
  2. count_hits(hit_times, start, end): Return the number of hits whose timestamps are between start and end, inclusive.

The input timestamps are integer seconds. A timestamp may occur multiple times, and every hit must be counted. The query function must not modify the list.

Formal Specification

  • hit_times is a sorted list of integers representing recorded hit timestamps.
  • timestamp, start, and end are integers.
  • record_hit returns the updated list after inserting the timestamp in sorted order.
  • count_hits returns an integer count.
  • Assume start <= end.

Constraints

  • 0 <= len(hit_times) <= 10^5
  • 0 <= timestamp, start, end <= 10^9
  • hit_times is sorted before each call to count_hits
  • Duplicate timestamps are allowed
  • start <= end

Function Signature

def count_hits(hit_times, start, end):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output