← All Lessons Lesson 59 / 68
Lesson 59

Finding Employee Free Time: Merging Meeting Intervals

Finding the Free Time Between Meetings

Imagine a company where many employees have meetings throughout the day. Each meeting has a start time and an end time. Our job is to find the moments when *every* employee is free at the same time — no one is in any meeting.

What the problem is asking

You are given a list of meetings. Each meeting is written as a pair [start, end], and we always know that the start comes before the end (si < ei).

For example:

meetings = [[1, 4], [2, 3], [3, 4], [4, 6], [8, 9]]

We want to find the gaps between meetings — the time slots where no meeting is happening at all. Those gaps are the free time.

One important rule: we only care about the empty spaces *inside* the overall span of meetings.

  • Time before the very first meeting starts → ignore it.
  • Time after the very last meeting ends → ignore it.

So we look only at the quiet moments squeezed between busy moments.

Walking through the example

Let's lay the meetings out on a timeline:

  • [1, 4] → busy from 1 to 4
  • [2, 3] → busy from 2 to 3 (this is already inside [1, 4])
  • [3, 4] → busy from 3 to 4 (also inside [1, 4])
  • [4, 6] → busy from 4 to 6
  • [8, 9] → busy from 8 to 9

If you combine all the overlapping and touching meetings, the busy time runs continuously from 1 all the way to 6. Then there is a quiet stretch, and the next meeting starts at 8.

So the free time is the gap from 6 to 8, written as [6, 8].

Output: [[6, 8]]

That single gap is the only time when *all* employees are guaranteed to be free.

The key idea: when do two meetings overlap?

To find the gaps, we first need to know how to join meetings that touch or overlap. The page gives us the rule:

> Two intervals [s1, e1] and [s2, e2] overlap if e1 >= s2.

In plain words: if the first meeting ends *at or after* the second meeting begins, the two meetings touch or run into each other, so we treat them as one big busy block.

This is why [1, 4], [2, 3], [3, 4], and [4, 6] all merge together — each one ends at or after the next one starts, so they form one unbroken busy period from 1 to 6.

A simple plan to solve it

Here is the intuition for how the function employeeFreeTime works:

  1. Sort all the meetings by their start time. This lines them up in order along the timeline.
  2. Merge any meetings that overlap or touch (using the e1 >= s2 rule) into single busy blocks.
  3. Look at the gaps between one merged block ending and the next one beginning. Each such gap is a piece of free time.

The starter code looks like this:

using namespace std;
class Solution {
public:
    vector<vector<int>> employeeFreeTime(vect
};

The function returns a vector<vector<int>> — a list of intervals, where each inner list is one free-time slot like [6, 8].

Why this matters

This is a classic interval problem. The same merging idea appears again and again: scheduling rooms, booking systems, and calendars all rely on sorting intervals by start time and then deciding when one busy block ends and the next begins. Master this pattern once, and many similar problems become easy.

Finding Employee Free Time: Merging Meeting Intervals
Diagram — click to zoom (scroll / drag to pan)