Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Find Narrowest Appointment Slot
00:00
5 left

Find Narrowest Appointment Slot

MediumPython

Problem

Headway needs to recommend an available appointment window near a member's preferred start time. Given available appointment intervals, a requested appointment duration, and a preferred start time, return the best interval that can contain the appointment.

An interval is represented as [start, end], where both values are integer minutes from the beginning of the day and start < end. An interval is feasible when end - start >= duration.

Rank feasible intervals using these rules, in order:

  1. Minimize the absolute difference between the interval's start and preferred_start.
  2. If tied, choose the narrower interval, meaning the smaller value of end - start.
  3. If still tied, choose the interval with the earlier start.

Return the original interval as [start, end]. Return [] if no interval can fit the requested duration.

Constraints

  • 0 <= len(slots) <= 10^5
  • 0 <= start < end <= 1440 for every interval
  • 1 <= duration <= 1440
  • 0 <= preferred_start <= 1440
  • Intervals may overlap

Function Signature

def choose_appointment_slot(slots, duration, preferred_start):
Interviewer

Your question is Find Narrowest Appointment 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.