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.
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.
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.
def find_narrowest_slot(appointments, work_start, work_end, duration):