Bird
Raised Fist0
Interview PrepintervalshardFacebookAmazonGoogle

Employee Free Time

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
</>
IDE
def employeeFreeTime(schedule: list[list[list[int]]]) -> list[list[int]]:public List<List<Integer>> employeeFreeTime(List<List<List<Integer>>> schedule)vector<vector<int>> employeeFreeTime(vector<vector<vector<int>>> &schedule)function employeeFreeTime(schedule)
def employeeFreeTime(schedule):
    # Write your solution here
    pass
class Solution {
    public List<List<Integer>> employeeFreeTime(List<List<List<Integer>>> schedule) {
        // Write your solution here
        return new ArrayList<>();
    }
}
#include <vector>
using namespace std;

vector<vector<int>> employeeFreeTime(vector<vector<vector<int>>> &schedule) {
    // Write your solution here
    return {};
}
function employeeFreeTime(schedule) {
    // Write your solution here
}
Coming soon
0/10
Common Bugs to Avoid
Wrong: [[2,3]]Returning gaps inside busy intervals due to incorrect merging.Sort all intervals and merge overlapping or touching intervals before finding gaps.
Wrong: [[2,2]]Including zero-length intervals as free time.Filter out intervals where start == end before returning.
Wrong: [[3,4],[5,6]]Treating touching intervals as free time gaps.Merge touching intervals as continuous busy time, no gaps between [1,2] and [2,3].
Wrong: [[1,10]]Returning entire timeline as free time when employees have no intervals.Return empty list if no finite free intervals exist; infinite free time not representable.
Wrong: [[3,4],[7,8]]Greedy approach missing intervals causing false free time.Merge all intervals from all employees before detecting gaps.
Test Cases
t1_01basic
Input[[[1,2],[5,6]],[[1,3]],[[4,10]]]
Expected[[3,4]]

Merging all intervals gives [1,3], [4,10], [5,6] merged into [1,3] and [4,10]. The gap between 3 and 4 is free time for all employees.

t1_02basic
Input[[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
Expected[[5,6],[7,9]]

Merged intervals are [1,5], [6,7], [9,12]. Free times are [5,6] and [7,9].

t2_01edge
Input[]
Expected[]

No employees means no busy intervals, so no finite free intervals to return.

t2_02edge
Input[[[1,5]]]
Expected[]

Only one employee with one interval means no common free time inside that interval; outside is infinite and not returned.

t2_03edge
Input[[[1,2],[2,3]],[[2,3],[3,4]]]
Expected[]

Intervals that touch but do not overlap produce no free time between them.

t2_04edge
Input[[],[],[]]
Expected[]

Employees with no intervals means entire timeline is free but infinite intervals are not returned.

t3_01corner
Input[[[1,10]],[[2,3],[4,5],[6,7],[8,9]],[[1,2],[3,4],[5,6],[7,8],[9,10]]]
Expected[]

All employees' intervals cover the entire range [1,10] without gaps, so no free time.

t3_02corner
Input[[[1,3],[5,7]],[[2,4],[6,8]],[[1,2],[4,5],[7,9]]]
Expected[]

Intervals merge to [1,4],[5,9]. The gaps at 4 and 9 are zero length, so no free time intervals of positive length exist; output is empty.

t3_03corner
Input[[[1,2],[5,6]],[[1,3]],[[4,10]]]
Expected[[3,4]]

This test checks that the solution does not confuse 0/1 knapsack style inclusion with interval merging; intervals must be merged by timeline, not by employee.

t4_01performance
Input{"_description":"n=100000 at constraint boundary - executor generates this input"}
⏱ Performance - must finish in 2000ms

Input with 100000 intervals tests O(N log N) sweep line approach; brute force O(N*M*T) will TLE.

Practice

(1/5)
1. You are given a list of trips, each with a number of passengers, a start location, and an end location. You need to determine if a vehicle with a fixed capacity can carry all passengers without exceeding capacity at any point along the route. Which algorithmic approach best guarantees an optimal and efficient solution?
easy
A. Brute force simulation by incrementing passenger count at every location for each trip
B. Greedy algorithm that picks trips with earliest end location first
C. Dynamic programming to find maximum passengers at each location using state transitions
D. Sweep line algorithm that processes passenger count changes at trip start and end points

Solution

  1. Step 1: Understand problem constraints

    The problem requires tracking passenger counts over intervals defined by start and end locations.
  2. Step 2: Identify suitable algorithm

    Sweep line processes events (passenger changes) in sorted order, efficiently tracking capacity usage without simulating every location.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Sweep line handles interval events optimally [OK]
Hint: Intervals with start/end events -> sweep line [OK]
Common Mistakes:
  • Assuming greedy earliest end works for capacity constraints
  • Thinking DP is needed for counting passengers
  • Using brute force for large location ranges
2. You are given a set of intervals representing horizontal balloons on a number line. Each balloon can be burst by shooting an arrow vertically through any point covered by the balloon's interval. What is the most efficient approach to find the minimum number of arrows needed to burst all balloons?
easy
A. Sort intervals by their end coordinate and greedily shoot arrows at the earliest possible end to cover maximum balloons.
B. Sort intervals by their start coordinate and greedily shoot arrows at the start of each balloon.
C. Use dynamic programming to find the maximum number of overlapping intervals and subtract from total balloons.
D. Use a brute force nested loop to check all pairs of intervals for overlaps and count arrows accordingly.

Solution

  1. Step 1: Understand the problem constraints

    The problem requires minimizing arrows to burst all balloons, which translates to covering intervals with minimum points.
  2. Step 2: Identify the optimal greedy strategy

    Sorting by end coordinate allows shooting an arrow at the earliest finishing balloon's end, covering all overlapping balloons starting before that point.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sorting by end coordinate ensures minimal arrows by maximizing coverage [OK]
Hint: Sort intervals by end to greedily cover overlaps [OK]
Common Mistakes:
  • Sorting by start coordinate and shooting at start misses optimal overlaps
  • Using DP unnecessarily complicates the problem
  • Brute force is too slow for large inputs
3. What is the time complexity of the addNum operation in the optimal balanced tree approach for maintaining disjoint intervals, assuming there are n intervals stored?
medium
A. O(n) because intervals need to be scanned linearly for merging
B. O(log n) due to binary search and limited merging operations
C. O(n log n) because each insertion requires sorting intervals
D. O(1) since insertion is always at the end or start

Solution

  1. Step 1: Identify main operations in addNum

    addNum uses binary search (bisect_left) to find insertion index in O(log n).
  2. Step 2: Analyze merging cost

    Merging involves at most constant number of interval merges (left, right, or both), so O(1) extra work.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Binary search dominates, merges are constant time [OK]
Hint: Binary search + constant merges -> O(log n) [OK]
Common Mistakes:
  • Assuming linear scan for merging
  • Confusing sorting cost per insertion
  • Ignoring constant merge steps
4. The following code attempts to determine if a person can attend all meetings. Identify the line containing the subtle bug that causes incorrect results for intervals that just touch (e.g., [1,5] and [5,10]).
from typing import List

def can_attend_meetings(intervals: List[List[int]]) -> bool:
    intervals.sort(key=lambda x: x[0])
    end = float('-inf')
    for interval in intervals:
        if interval[0] <= end:
            return False
        end = interval[1]
    return True
medium
A. Line 6: if interval[0] <= end:
B. Line 3: intervals.sort(key=lambda x: x[0])
C. Line 5: for interval in intervals:
D. Line 7: end = interval[1]

Solution

  1. Step 1: Understand the overlap condition

    Meetings that just touch (e.g., end=5 and start=5) should not be considered overlapping.
  2. Step 2: Identify the bug

    The condition uses <= which incorrectly treats touching intervals as overlapping. It should use < instead.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Changing <= to < fixes false positives on touching intervals [OK]
Hint: Overlap check must use <, not <= [OK]
Common Mistakes:
  • Using <= causes false overlap detection on touching intervals
5. What is the time complexity of the optimal in-place merge intervals algorithm that first sorts the intervals and then merges them in one pass?
medium
A. O(n log n) due to the sorting step dominating the runtime
B. O(n) because merging is done in a single pass
C. O(n²) because each interval is compared with all others during merging
D. O(n log n) because merging requires binary searches for overlaps

Solution

  1. Step 1: Identify the sorting step complexity.

    Sorting n intervals by start time takes O(n log n) time.
  2. Step 2: Analyze the merging step complexity.

    Merging intervals in one pass is O(n), which is dominated by sorting.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sorting dominates overall complexity, merging is linear. [OK]
Hint: Sorting dominates, merging is linear [OK]
Common Mistakes:
  • Ignoring sorting cost
  • Assuming nested comparisons cause O(n²)