Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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 feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Schedule With Narrowest Slot
00:00
5 left

Schedule With Narrowest Slot

MediumPython

Problem

Headway needs to place a new patient appointment within a provider's working hours. Given existing appointments, find the narrowest available time slot that can fit the requested duration.

Appointments may overlap or touch, and they are not guaranteed to be sorted. Return the new appointment as [start, end], where start and end are integer minutes from midnight. If multiple slots have the same smallest available gap, choose the earliest one. Return [] if no gap is large enough.

Formal Specification

Implement find_narrowest_slot(appointments, work_start, work_end, duration), where appointments is a list of [start, end] intervals, and the remaining arguments are integers. The function returns a two-element list representing the scheduled appointment, or an empty list when scheduling is impossible. Schedule the appointment at the beginning of the selected free gap.

Constraints

  • 0 <= len(appointments) <= 10^5
  • work_start < work_end
  • 0 < duration <= work_end - work_start
  • Every appointment satisfies work_start <= start < end <= work_end
  • Appointment times are integer minutes from midnight

Function Signature

def find_narrowest_slot(appointments, work_start, work_end, duration):
Interviewer

Your question is Schedule With Narrowest Slot. 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.