Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Peak Usage Interval Algorithm
00:00
5 left

Peak Usage Interval Algorithm

MediumPython

Problem

Lyra Health wants to identify the busiest short periods in an activity timeline. Given usage log timestamps, an analysis window, and an interval length, return every candidate interval containing the maximum number of logs.

Candidate intervals must start at the analysis window's start or at a timestamp present in the input. Intervals use half-open boundaries [start, end), so a log at end is excluded. Return intervals as [start, end] pairs, sorted by start time. If no timestamps fall inside the analysis window, return [].

Formal Specification

Implement peak_usage_intervals(timestamps, window_start, window_end, interval_length).

  • timestamps is a list of integer timestamps.
  • window_start and window_end define the analysis range [window_start, window_end).
  • interval_length is a positive integer.
  • Every returned interval must fit entirely inside the analysis range.
  • Return all candidate intervals whose log count equals the maximum count.

Constraints

  • 0 <= len(timestamps) <= 10^5
  • window_start < window_end <= 10^9
  • 1 <= interval_length <= window_end - window_start
  • 0 <= timestamps[i] <= 10^9
  • Duplicate timestamps represent separate usage events.

Function Signature

def peak_usage_intervals(timestamps, window_start, window_end, interval_length):
Interviewer

Your question is Peak Usage Interval Algorithm. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.