Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #759: Employee Free Time

In this guide, we solve Leetcode #759 Employee Free Time in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Hard
  • Premium: Yes
  • Tags: Array, Sorting, Line Sweep, Heap (Priority Queue)

Intuition

We need to repeatedly access the smallest or largest element as the input changes.

A heap provides fast insertions and removals while keeping order.

Approach

Push candidates into the heap as you scan, and pop when you need the best element.

Keep the heap size bounded if the problem requires a top-k structure.

Steps:

  • Push candidates into a heap.
  • Pop the best candidate when needed.
  • Maintain heap size or invariants.

Example

Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]] Output: [[3,4]] Explanation: There are a total of three employees, and all common free time intervals would be [-inf, 1], [3, 4], [10, inf]. We discard any intervals that contain inf as they aren't finite.

Python Solution

""" # Definition for an Interval. class Interval: def __init__(self, start: int = None, end: int = None): self.start = start self.end = end """ class Solution: def employeeFreeTime(self, schedule: "[[Interval]]") -> "[Interval]": intervals = [] for e in schedule: intervals.extend(e) intervals.sort(key=lambda x: (x.start, x.end)) merged = [intervals[0]] for x in intervals[1:]: if merged[-1].end < x.start: merged.append(x) else: merged[-1].end = max(merged[-1].end, x.end) ans = [] for a, b in pairwise(merged): ans.append(Interval(a.end, b.start)) return ans

Complexity

The time complexity is O(mnlog⁡(mn))O(mn \log(mn))O(mnlog(mn)) and the space complexity is O(mn)O(mn)O(mn), where mmm is the number of employees and nnn is the number of working intervals per employee. The space complexity is O(mn)O(mn)O(mn), where mmm is the number of employees and nnn is the number of working intervals per employee.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy