This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
Take all the intervals and do an "events" (or "line sweep") approach - an event of (x, OPEN) increases the number of active intervals, while (x, CLOSE) decreases it. Processing in sorted order from left to right, if the number of active intervals is zero, then you crossed a region of common free time.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Sorting intervals ensures that working periods are processed in chronological order. This makes it easy to merge overlapping intervals and detect gaps between them. Those gaps correspond to common free time across employees.
Yes, interval scheduling and merging problems like Employee Free Time are common in technical interviews at large tech companies. They test understanding of sorting, heaps, and interval processing techniques.
A priority queue (min-heap) works well for merging schedules from multiple employees. It allows efficient retrieval of the earliest starting interval during a k-way merge. This reduces the complexity to O(N log K) where K is the number of employees.
A common optimal approach is to collect all employee intervals, sort them by start time, and merge overlapping intervals. The gaps between merged intervals represent shared free time. This approach is straightforward and runs in O(N log N) time.