KatInterview

Open Half-Hour Slots from Weekly Hours and Bookings

Greedy & IntervalsAuto-graded · 19 test cases
MediumAuto-gradedGreedy & Intervalsintervalsdatetimesimulation

Last updated

Expand a weekly schedule over a date range and subtract booked intervals to list every free, half-hour-aligned 30-minute slot in chronological order.

Open Half-Hour Slots

A practice owner publishes a weekly template of working hours. Some time ranges are already booked. Given a range of calendar dates, list every 30-minute slot that is still free.

Inputs

  • weekly_hours: a dictionary mapping a weekday key to a list of working windows.
    • Keys are strings: "0" is Monday, "1" is Tuesday, ..., "6" is Sunday.
    • Each window is [start, end], with both times as "HH:MM" strings on a 24-hour clock.
    • A weekday with no key has no working hours.
  • bookings: a list of [start, end] pairs. Each is a datetime string in the form YYYY-MM-DDTHH:MM:SS.
  • range_start, range_end: ISO dates (YYYY-MM-DD). Both are inclusive.

Task

Return all free slots, where each slot is a two-element list [slot_start, slot_end] of strings in YYYY-MM-DDTHH:MM:SS format.

For each date from range_start to range_end, take the windows listed for that date's weekday. A window is interpreted on that same date.

A slot is returned when all of these hold:

  1. It lasts exactly 30 minutes.
  2. It lies completely inside one working window, so the slot start is no earlier than the window start and the slot end is no later than the window end.
  3. Its start minute is 00 or 30.
  4. It is not blocked by any booking.

Rules

  • Slot placement inside a window. The first slot in a window starts at the window's start time if that is already on :00 or :30. Otherwise it starts at the next :00 or :30 after it. Later slots follow back-to-back, 30 minutes apart. Any leftover time shorter than 30 minutes at the end of a window is unused. For example, 09:15–10:00 yields only 09:30–10:00.
  • Blocking. A booking [b_start, b_end] blocks a slot [s, e] exactly when b_start < e and b_end > s. Intervals that merely touch do not block. A booking ending at 09:30 does not block the slot starting at 09:30.
  • Bookings may be given in any order, may overlap each other, and may fall on dates outside the range, in which case they have no effect. A booking may cross the edges of a working window.
  • A window whose end is not later than its start is ignored.
  • If range_start is later than range_end, return an empty list.
  • The result must be sorted chronologically by slot start.

Constraints

  • All times in weekly_hours are valid "HH:MM" values from "00:00" to "23:59".
  • Windows listed for the same weekday do not overlap one another.
  • The date range spans at most a few hundred days, so each date can be handled one at a time.
  • The total number of bookings is at most a few thousand.
  • Dates and datetimes are valid ISO strings. Booking times have no time-zone information.

Examples

Example 1

Input: list_open_slots({'0': [['09:00', '10:00']]}, [['2023-10-02T09:30:00', '2023-10-02T10:00:00']], '2023-10-02', '2023-10-02')

Output: [['2023-10-02T09:00:00', '2023-10-02T09:30:00']]

Example 2

Input: list_open_slots({'0': [['09:00', '11:00']]}, [], '2023-10-02', '2023-10-02')

Output: [['2023-10-02T09:00:00', '2023-10-02T09:30:00'], ['2023-10-02T09:30:00', '2023-10-02T10:00:00'], ['2023-10-02T10:00:00', '2023-10-02T10:30:00'], ['2023-10-02T10:30:00', '2023-10-02T11:00:00']]

Hints

Hint 1

Walk through the dates one day at a time and use the date's weekday to look up that day's working windows.

Hint 2

Round each window's start up to the next multiple of 30 minutes, then step forward 30 minutes at a time while the whole slot still fits before the window end.

Hint 3

A slot is blocked only if a booking starts before the slot ends and ends after the slot starts. Sorting the bookings by start lets you stop checking early once a booking starts at or after the slot's end.