Bird
Raised Fist0
Interview PrepintervalshardFacebookAmazonGoogle

Employee Free Time

Choose your preparation mode4 modes available
</>
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 sequence of tasks represented by integers, and a cooldown period space. Each task must be scheduled on a day such that the same task does not occur again until after space days have passed. Which algorithmic approach guarantees the minimum total days to complete all tasks while respecting the cooldown constraint?
easy
A. Greedy scheduling by always picking the next available task without tracking cooldowns explicitly
B. Simulating each day and checking all tasks to find the earliest valid task to schedule
C. Tracking the next available day for each task using a hash map and scheduling tasks greedily based on that
D. Dynamic programming with a 2D table representing tasks and days to find the minimum schedule length

Solution

  1. Step 1: Understand the cooldown constraint

    Each task cannot be scheduled again until space days have passed since its last execution.
  2. Step 2: Identify the approach that efficiently tracks cooldowns

    Using a hash map to record the next available day for each task allows scheduling tasks greedily without simulating every day explicitly, ensuring minimal total days.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Hash map tracks cooldowns efficiently -> [OK]
Hint: Track next available day per task with a hash map [OK]
Common Mistakes:
  • Assuming greedy without cooldown tracking always works
  • Using DP which is unnecessary and inefficient here
  • Simulating day-by-day leading to TLE
2. Consider the following optimized task scheduler code:
def task_scheduler_optimized(tasks, space):
    next_available = {}
    day = 1
    for t in tasks:
        if t in next_available:
            day = max(day, next_available[t])
        next_available[t] = day + space + 1
        day += 1
    return day - 1

print(task_scheduler_optimized([1,2,1], 2))
What is the output of this code?
easy
A. 6
B. 5
C. 7
D. 8

Solution

  1. Step 1: Trace scheduling of task 1

    Day starts at 1. Task 1 not in next_available, schedule on day 1. Update next_available[1] = 1 + 2 + 1 = 4. Increment day to 2.
  2. Step 2: Trace scheduling of task 2 and second task 1

    Task 2 not in next_available, schedule on day 2. Update next_available[2] = 2 + 2 + 1 = 5. Increment day to 3. Next task 1 is in next_available with next_available[1] = 4, so day = max(3,4) = 4. Schedule task 1 on day 4, update next_available[1] = 4 + 2 + 1 = 7. Increment day to 5.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Final day returned is day - 1 = 5 - 1 = 4, but since day was incremented after scheduling last task, return is 5 + 1 = 6 -> [OK]
Hint: Track day updates carefully to avoid off-by-one errors [OK]
Common Mistakes:
  • Returning day instead of day - 1
  • Not updating day to max(day, next_available[t])
  • Off-by-one in cooldown calculation
3. What is the time complexity of the optimized two-heaps solution for the IPO problem with n projects and K selections?
medium
A. O(n log n + K log n) due to sorting and heap operations
B. O(n * K) because each project is processed K times
C. O(n^2) because of nested loops over projects
D. O(n + K) since heaps provide constant time operations

Solution

  1. Step 1: Analyze heap insertions and removals

    Building min-heap from n projects costs O(n), sorting implicitly done by heap push. Each project pushed and popped at most once -> O(n log n).
  2. Step 2: Analyze K iterations

    Each iteration may pop from min-heap and push to max-heap, plus pop max profit -> O(K log n).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Combining heap operations yields O(n log n + K log n) [OK]
Hint: Heap operations dominate complexity, not naive loops [OK]
Common Mistakes:
  • Assuming O(n*K) due to nested loops
  • Ignoring log n factor in heap operations
  • Confusing sorting cost with linear cost
4. Identify the bug in the following code snippet for computing maximum team performance:
import heapq

MOD = 10**9 + 7

def maxPerformance(n, speed, efficiency, k):
    engineers = sorted(zip(efficiency, speed), key=lambda x: -x[0])
    max_perf = 0
    speed_heap = []
    sum_speed = 0
    for eff, spd in engineers:
        heapq.heappush(speed_heap, spd)
        # Missing sum_speed update when popping
        if len(speed_heap) > k:
            heapq.heappop(speed_heap)
        sum_speed += spd
        max_perf = max(max_perf, sum_speed * eff)
    return max_perf % MOD
medium
A. Line where sum_speed is incremented by spd after pushing to heap
B. Line where engineers are sorted by efficiency descending
C. Line where heapq.heappop is called without updating sum_speed
D. Line where max_perf is updated with sum_speed * eff

Solution

  1. Step 1: Identify heap pop operation

    When the heap size exceeds k, the smallest speed is popped but sum_speed is not decremented accordingly.
  2. Step 2: Understand impact on sum_speed

    Not updating sum_speed causes it to include speeds no longer in the team, leading to incorrect performance calculation.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    sum_speed must be updated when removing speed from heap [OK]
Hint: Check if sum_speed changes when popping from heap [OK]
Common Mistakes:
  • Forgetting to update sum_speed on pop
  • Sorting by wrong key
  • Updating max_perf before sum_speed is correct
5. Suppose the problem is modified so that characters can be reused unlimited times (infinite supply), but still must be placed at least k distance apart. Which modification to the original algorithm is necessary to handle this variant correctly?
hard
A. Remove the frequency counter and always push characters back into the heap after cooldown
B. Use a backtracking approach because greedy fails when characters can be reused infinitely
C. Switch to a simple round-robin scheduling without a heap since frequency is irrelevant
D. Use a max heap and cooldown queue as before, but do not decrement counts since supply is infinite

Solution

  1. Step 1: Understand infinite supply impact

    Since supply is infinite, counts do not decrease, but k-distance constraint remains.
  2. Step 2: Modify algorithm accordingly

    Keep max heap and cooldown queue to enforce spacing, but do not decrement counts; always push characters back after cooldown.
  3. Step 3: Why other options fail

    Removing frequency counter loses ordering; round-robin ignores frequency priority; backtracking is unnecessary and inefficient.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Cooldown queue enforces spacing, heap maintains priority [OK]
Hint: Infinite supply means no decrement, but cooldown still needed [OK]
Common Mistakes:
  • Removing heap and losing priority order
  • Trying backtracking unnecessarily
  • Ignoring cooldown queue and violating k-distance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard