Locate Shared Free Time Slots Across Multiple Schedules
Greedy & IntervalsAuto-graded · 18 test casesLast updated
Tests interval merging to identify time windows where all participants are simultaneously unoccupied for a minimum duration.
You're building a meeting scheduler that needs to identify time windows when all participants can meet. You're given the occupied time blocks for several people, a working day boundary, and a minimum meeting duration. Your task is to find every time period within the working day when all participants are simultaneously available for at least the required duration.
Each participant's schedule is a list of non-overlapping time intervals [start, end] representing when they are busy, sorted by start time. Time is measured in integer units (such as minutes from midnight).
Write a function that returns all qualifying available time slots in chronological order.
Input
schedules: A list of schedules, whereschedules[i]contains the busy intervals for participanti. Each interval is represented as[start, end]where the participant is unavailable during the half-open range[start, end)(busy fromstartinclusive toendexclusive).working_hours: A two-element list[day_start, day_end]that defines the working day boundaries. Only consider availability within[day_start, day_end).duration: A positive integer specifying the minimum length required for a valid meeting slot.
Output
Return a list of intervals [start, end] where:
- Every participant is free during the entire interval
[start, end) - The interval is completely within
[day_start, day_end) - The interval length
end - startis at leastduration
The output intervals should be non-overlapping and sorted by start time. Each returned interval represents a maximal contiguous free period (not truncated to exactly duration length).
Constraints
1 <= len(schedules) <= 100- Total busy intervals across all participants
<= 10^4 0 <= day_start < day_end <= 10^61 <= duration <= day_end - day_start- For each interval
[start, end]:start < end - Intervals within a single participant's schedule do not overlap
Important Notes
- Time intervals use half-open semantics:
[start, end)means busy fromstart(inclusive) toend(exclusive) - Two intervals
[a, b)and[b, c)are considered adjacent with no gap between them (the time pointbis busy in the second interval) - Busy intervals may extend outside
[day_start, day_end)and should be clipped to the working hours boundary - If all participants have empty schedules (completely free), the entire working window is available
Examples
Example 1
Input: find_available_slots([[[90, 150], [180, 240]], [[120, 160], [270, 330]]], [60, 360], 30)
Output: [[60, 90], [240, 270], [330, 360]]
Example 2
Input: find_available_slots([[[0, 300]]], [100, 200], 15)
Output: []
Hints
Hint 1
Rather than computing free time for each person and intersecting, collect all busy intervals from everyone into one list.
Hint 2
After gathering busy intervals, sort and merge overlapping or adjacent ones to identify when at least one person is unavailable.
Hint 3
Available periods are the gaps between merged busy intervals that fall within the working hours and meet the duration requirement.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A time is available for a meeting only when no participant is busy at that moment. Rather than calculating each person's free intervals and intersecting them all, we work with the opposite: aggregate every busy interval from every participant, merge them, and then identify the gaps.
The solution proceeds in three steps:
-
Collect and clip: Gather all busy intervals from every participant into a single list. Clip each interval to fit within the working hours boundary. Remove any interval that becomes empty after clipping (where
start >= end). -
Sort and merge: Sort all busy intervals by start time. Then merge intervals that overlap or touch. Two intervals touch when one ends exactly where another begins, leaving no gap. The merge condition
start <= merged[-1][1]correctly handles this: if a new interval starts at or before the previous one ends, they connect. This produces a list of disjoint busy blocks. -
Extract gaps: Walk through the merged busy intervals, tracking the current free position starting from
day_start. For each busy block, the gap before it (from the current position to the block's start) is a candidate free slot. Include it only if its length meets the duration requirement. Update the position to the busy block's end. After processing all busy blocks, check for a final gap today_end.
Correctness
The union of all busy intervals represents exactly when at least one participant is unavailable. The complement of this union within the working window gives the times when everyone is free. Merging computes this union efficiently.
The merge logic with start <= merged[-1][1] correctly treats adjacent intervals (such as [100, 150) and [150, 200)) as contiguous busy time with no free gap between them, which aligns with half-open interval semantics.
Clipping ensures we only consider times within the working window. The gap extraction accounts for the initial gap (before any busy intervals), intermediate gaps (between consecutive busy intervals), and the final gap (after the last busy interval).
Complexity
- Time: O(N log N), where N is the total number of busy intervals across all participants. Sorting is the bottleneck; collection, clipping, merging, and gap extraction are each linear.
- Space: O(N) for storing collected and merged intervals, plus the output.
Solution
from typing import List
def find_available_slots(schedules: List[List[List[int]]], working_hours: List[int], duration: int) -> List[List[int]]:
day_start, day_end = working_hours
# Collect all busy intervals and clip them to working hours
busy = []
for schedule in schedules:
for start, end in schedule:
start = max(start, day_start)
end = min(end, day_end)
if start < end:
busy.append((start, end))
# Sort and merge overlapping/touching intervals
busy.sort()
merged = []
for start, end in busy:
if merged and start <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], end)
else:
merged.append([start, end])
# Find gaps of sufficient length
free = []
cursor = day_start
for start, end in merged:
if start - cursor >= duration:
free.append([cursor, start])
cursor = max(cursor, end)
if day_end - cursor >= duration:
free.append([cursor, day_end])
return free