KatInterview

Locate Shared Free Time Slots Across Multiple Schedules

Greedy & IntervalsAuto-graded · 18 test cases
EasyAuto-gradedGreedy & Intervalsintervalsmergingscheduling

Last 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, where schedules[i] contains the busy intervals for participant i. Each interval is represented as [start, end] where the participant is unavailable during the half-open range [start, end) (busy from start inclusive to end exclusive).
  • 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 - start is at least duration

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^6
  • 1 <= 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 from start (inclusive) to end (exclusive)
  • Two intervals [a, b) and [b, c) are considered adjacent with no gap between them (the time point b is 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.