Employee Free Time
Problem Statement
You get the work schedule of k employees. schedule[e] is the list of times employee e is busy. Each employee's list is sorted and its intervals don't overlap.
Return every stretch of time when all employees are free at once, as a sorted list of intervals. Only return gaps of positive, finite length: skip the endless time before everyone's first interval and after everyone's last one, and skip a "gap" like [5,5] where one interval ends exactly when another starts.
About the signature: on LeetCode the input is vector<vector<Interval>>, where Interval is a small class LeetCode gives you with two fields, start and end. So schedule[e][i].start is when employee e's i-th busy interval begins. The answer is a vector<Interval>. Our code shows Interval as a plain struct only so it is readable on its own.
schedule: a list of k lists. schedule[e] holds employee e's busy intervals, sorted by start.
The common free intervals, sorted by start. It can be empty.
Together, people are busy during [1,3] and [4,10]. The only finite gap is [3,4]. Time before 1 and after 10 is endless, so it is left out.
Busy time joined together: [1,5], [6,7], [9,12]. The gaps between them are [5,6] and [7,9].
- • 1 ≤ schedule.length ≤ 50
- • 1 ≤ schedule[i].length ≤ 50
- • 0 ≤ schedule[i][j].start < schedule[i][j].end ≤ 10^8
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
A gap can only start where some interval ends; test each end against all intervals
Sort all intervals by start; walk with lastEnd and report every gap
Merge the k sorted lists with a min-heap; report a gap whenever the next start passes lastEnd
Put every busy interval into one list. A free gap can only begin at some interval's end. For each distinct end e, check all intervals: if nobody is busy at e (no interval has start ≤ e < end), the gap runs from e to the next start after e. If nothing starts after e, the gap is endless, so skip it.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.