KatInterview

Compute Free Windows from Schedule Constraints

Greedy & IntervalsAuto-graded · 21 test cases
MediumAuto-gradedGreedy & Intervalsintervalssortingstring

Last updated

Compute unreserved time windows for specified daily schedules given opening hours and existing bookings.

You are given daily operating hours and existing bookings for a service. Your task is to compute all free time intervals within operating hours for each day.

Input Format

  1. hours: A dictionary mapping day names (e.g., "Mon") to a pair [open_time, close_time].
  2. reservations: A dictionary mapping day names to a list of existing bookings [[start1, end1], [start2, end2], ...].

Times in hours and reservations can be specified as either HH:MM formatted strings (e.g., "11:00") or as integer minutes from midnight (e.g., 660).

Requirements & Behavior

  • All time intervals are treated as half-open [start, end).
  • If close_time <= open_time for a day, that day has no available slots (return an empty list []).
  • Bookings must be clipped to the day's operating hours [open_time, close_time]. Any block or portion of a block outside these hours should be ignored.
  • Existing bookings may be unsorted, overlap, or touch at boundaries (e.g., one ending at 13:00 and another starting at 13:00).
  • Overlapping and adjacent reserved intervals must be merged before identifying free gaps.
  • Only days present in the hours dictionary should appear in the returned dictionary. If a day is in hours but absent from reservations, assume there are no bookings for that day.
  • Output times must be formatted as HH:MM strings with zero-padded two-digit hours and minutes.

Output Format

Return a dictionary matching each key in hours to a list of available [start_time, end_time] string pairs formatted as HH:MM.

Constraints

  • $1 \le \text{len}(hours) \le 7$
  • $0 \le \text{number of reservations per day} \le 10^4$
  • Times are non-negative and bounded within a single day ($0 \le \text{minutes} \le 1440$ or "00:00" to "24:00").

Examples

Example 1

Input: find_available_slots({'Mon': ['11:00', '22:00'], 'Tue': ['11:00', '22:00']}, {'Mon': [['12:00', '12:30'], ['18:00', '19:15']], 'Tue': []})

Output: {'Mon': [['11:00', '12:00'], ['12:30', '18:00'], ['19:15', '22:00']], 'Tue': [['11:00', '22:00']]}

Example 2

Input: find_available_slots({'Mon': ['09:00', '17:00']}, {'Mon': [['13:00', '14:00'], ['12:00', '13:30'], ['16:00', '17:00']]})

Output: {'Mon': [['09:00', '12:00'], ['14:00', '16:00']]}

Example 3

Input: find_available_slots({'Fri': [600, 1320], 'Sat': [720, 1440]}, {'Fri': [[660, 720], [720, 780], [900, 930]], 'Sat': [[1380, 1500]]})

Output: {'Fri': [['10:00', '11:00'], ['13:00', '15:00'], ['15:30', '22:00']], 'Sat': [['12:00', '23:00']]}

Hints

Hint 1

Convert all input timestamps (strings or integers) to unified integer minutes from midnight first.

Hint 2

Clip each reservation interval to [open_minutes, close_minutes] and ignore invalid intervals where start >= end.

Hint 3

Sort the clipped reservation intervals by start time and merge overlapping or adjacent intervals.

Hint 4

Use a sweep cursor starting at open_minutes to detect open gaps up to close_minutes, then format gaps back to HH:MM.