Employee Free Time - Problem

We are given a list schedule of employees, which represents the working time for each employee. Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order.

Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.

Note: Even though we are representing Intervals in the form [x, y], the objects inside are Intervals, not lists or arrays. For example, schedule[0][0].start = 1, schedule[0][0].end = 2, and schedule[0][0][0] is not defined. Also, we wouldn't include intervals like [5, 5] in our answer, as they have zero length.

Input & Output

Example 1 — Basic Case
$ Input: schedule = [[[1,3],[6,10]], [[2,4],[9,12]]]
Output: [[4,6]]
💡 Note: Employee 1 works [1,3] and [6,10]. Employee 2 works [2,4] and [9,12]. Merging all intervals: [1,4] and [6,12]. The gap between them is [4,6].
Example 2 — Multiple Gaps
$ Input: schedule = [[[1,3],[4,6]], [[2,5],[7,9]]]
Output: [[6,7]]
💡 Note: Employee 1: [1,3],[4,6]. Employee 2: [2,5],[7,9]. After merging: [1,6] and [7,9]. Free time is [6,7].
Example 3 — No Free Time
$ Input: schedule = [[[1,10]], [[2,6],[8,12]]]
Output: []
💡 Note: Employee 1 works [1,10], Employee 2 works [2,6],[8,12]. Combined coverage is [1,12] with no gaps.

Constraints

  • 1 ≤ schedule.length ≤ 50
  • 1 ≤ schedule[i].length ≤ 50
  • schedule[i][j].length == 2
  • 0 ≤ schedule[i][j][0] < schedule[i][j][1] ≤ 108

Visualization

Tap to expand
Employee Free Time INPUT Employee 1: [1,3] [6,10] Employee 2: [2,4] [9,12] Combined Timeline: 1 3 4 6 10 12 Input Schedule: [[[1,3],[6,10]], [[2,4],[9,12]]] ALGORITHM STEPS 1 Flatten All Intervals Merge all schedules into one list of intervals 2 Sort by Start Time [[1,3],[2,4],[6,10],[9,12]] 3 Merge Overlapping Combine into: [[1,4],[6,12]] 4 Find Gaps Gap between merged intervals = Free time for all Merge Visualization Before: After: [1,4] [6,12] GAP: [4,6] FINAL RESULT Free Time Found: 1 4 6 12 work work FREE Output: [[4, 6]] Verification: OK Interval [4,6] is the only time when ALL employees are free simultaneously Key Insight: The problem reduces to finding gaps in a merged interval list. By flattening all employee schedules, sorting by start time, and merging overlapping intervals, we transform the multi-employee problem into a single timeline. Free time = gaps between consecutive merged intervals. Time: O(n log n) TutorialsPoint - Employee Free Time | Optimal Solution
Asked in
Google 42 Facebook 35 Amazon 28 Microsoft 25
125.0K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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