Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Schedule With Narrowest Slot

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

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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output