Practice
Solution
Step 1: Understand problem requirements
The problem requires finding common free intervals across multiple employees, which means merging all intervals to identify gaps.Step 2: Evaluate approaches
Greedy earliest end time (A) does not guarantee merging all intervals correctly. DP (C) is unnecessary and inefficient here. Min-heap merging (D) is a valid approach but more complex than flattening and sorting. Flattening and sorting all intervals (B) ensures all intervals are processed in order, enabling correct gap detection.Final Answer:
Option B -> Option BQuick Check:
Flattening and sorting all intervals is the canonical approach for interval merging [OK]
- Assuming greedy earliest end time works for merging intervals
- Confusing DP with interval merging
- Thinking min-heap is always simpler
Solution
Step 1: Insert 1
low = [-1], high = [], median = 1 (only one element)Step 2: Insert 5
5 > 1, so push to high: high = [5], low = [-1]. Balanced heaps, median = (1 + 5)/2 = 3.0Step 3: Insert 2
2 <= 5 but > 1, so push to low: low = [-2, -1], high = [5]. Balance: low_size=2, high_size=1, median = top of low = 2.0Final Answer:
Option B -> Option BQuick Check:
Median after 3 inserts is 2.0 -> [OK]
- Not balancing heaps after insertion
- Incorrect median calculation for odd number of elements
- Confusing max-heap sign inversion
Solution
Step 1: Initialize heaps and result
max_start_heap = [(-3,1), (-2,0), (-1,2)], max_end_heap = [(-4,1), (-3,0), (-2,2)], result = [-1, -1, -1]Step 2: Process max_end_heap
Pop (-4,1): max_start_heap top is -3 < 4, continue no update.
Pop (-3,0): max_start_heap top -3 >= 3, pop candidates with start >= 3: (-3,1). min_start= -3, min_idx=1, result[0]=1.
Push back unused candidates (none).
Pop (-2,2): max_start_heap top -2 >= 2, pop candidates (-2,0), (-1,2). min_start= -1, min_idx=2, result[2]=2.
Push back (-2,0).Final Answer:
Option D -> Option DQuick Check:
Matches expected output for given input [OK]
- Off-by-one in indices
- Not pushing back unused intervals
- Confusing start and end heaps
Solution
Step 1: Analyze median calculation for even k
Median for even k is average of two middle elements, so sum must be divided by 2.Step 2: Identify bug
Line returning sum without division causes median to be twice the correct value.Final Answer:
Option A -> Option AQuick Check:
Missing division by 2 in even median calculation [OK]
- Forgetting to divide sum by 2 for even k median
- Incorrect index calculation for median
- Wrong removal or insertion causing imbalance
def task_scheduler_buggy(tasks, space):
next_available = {}
day = 1
for t in tasks:
if t in next_available:
day = next_available[t] # Bug here
next_available[t] = day + space + 1
day += 1
return day - 1
Solution
Step 1: Analyze day update logic
Line 6 assigns day = next_available[t], which can move day backwards if day is already ahead, violating cooldown constraints.Step 2: Correct approach
Use day = max(day, next_available[t]) to ensure day never moves backward and respects cooldown.Final Answer:
Option A -> Option AQuick Check:
Assignment without max causes scheduling too early -> [OK]
- Assigning day directly instead of max causes cooldown violation
- Misplacing day increment causing off-by-one errors
- Incorrect next_available update formula
