Employee Free Time - Problem
Find Common Free Time for All Employees

Imagine you're managing a team and need to schedule a meeting that works for everyone. You have each employee's working schedule as a list of time intervals, and you need to find all the time slots when everyone is free.

Given a list schedule where schedule[i] represents the working intervals for employee i, return all the common free time intervals for all employees. Each employee's intervals are non-overlapping and sorted.

Goal: Find time slots when ALL employees are simultaneously free
Input: List of employee schedules (each schedule is a list of intervals)
Output: List of intervals representing common free time

Note: Only return intervals with positive length (exclude zero-length intervals like [5,5])

Input & Output

example_1.py โ€” Basic Case
$ Input: schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
โ€บ Output: [[5,6],[7,9]]
๐Ÿ’ก Note: Employee 1 works [1,3] and [6,7]. Employee 2 works [2,4]. Employee 3 works [2,5] and [9,12]. The common free times are [5,6] (between 5-6, no one is working) and [7,9] (between 7-9, no one is working).
example_2.py โ€” Overlapping Schedules
$ Input: schedule = [[[1,3],[4,6]],[[2,5],[7,9]]]
โ€บ Output: [[6,7]]
๐Ÿ’ก Note: Employee 1 works [1,3] and [4,6]. Employee 2 works [2,5] and [7,9]. The merged busy time is [1,6] and [7,9], so the only free time is [6,7].
example_3.py โ€” No Free Time
$ Input: schedule = [[[1,10]],[[2,6],[8,10]],[[1,9]]]
โ€บ Output: []
๐Ÿ’ก Note: Employee 1 works [1,10], Employee 2 works [2,6] and [8,10], Employee 3 works [1,9]. When we merge all intervals, we get [1,10] with no gaps, so there's no common free time.

Visualization

Tap to expand
Employee Free Time FinderIndividual Schedules:Alice: [1,4][7,9]Bob: [2,5][10,12]Charlie: [1,6]Timeline:Merged: [1,6][7,9][10,12]FREEFREEFree Time Slots:[6,7][9,10]โ† Perfect for team meetings!
Understanding the Visualization
1
Collect All Busy Times
Gather all working intervals from every employee's schedule
2
Sort Chronologically
Arrange all intervals by their start time to process them in order
3
Merge Overlapping Periods
Combine overlapping work periods to get consolidated busy times
4
Find the Gaps
The spaces between merged busy periods are your meeting opportunities
Key Takeaway
๐ŸŽฏ Key Insight: Free time exists in the gaps between merged busy intervals. By combining all schedules and finding the holes, we discover when everyone is simultaneously available.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Where n is total number of intervals. Sorting dominates the complexity

n
2n
โšก Linearithmic
Space Complexity
O(n)

Need to store all intervals and the result list

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค schedule.length, schedule[i].length โ‰ค 50
  • 0 โ‰ค schedule[i][j].start < schedule[i][j].end โ‰ค 108
  • Each employee's intervals are non-overlapping and sorted
  • Only return positive-length intervals (exclude zero-length like [5,5])
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 25
52.0K Views
Medium Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen