Practice
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: Understand problem constraints
The problem requires selecting projects affordable at each step, maximizing capital after K picks.Step 2: Identify algorithmic pattern
Using two heaps allows efficient selection: min-heap to filter affordable projects by capital, max-heap to pick the most profitable among them, ensuring optimal capital gain.Final Answer:
Option D -> Option DQuick Check:
Two heaps efficiently handle affordability and profit selection [OK]
- Assuming sorting by profit alone suffices
- Using brute force without pruning
- Confusing DP with this greedy heap approach
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?Solution
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.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.Final Answer:
Option A -> Option AQuick 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]
- Returning day instead of day - 1
- Not updating day to max(day, next_available[t])
- Off-by-one in cooldown calculation
Solution
Step 1: Identify sorting cost
Sorting n engineers by efficiency takes O(n log n).Step 2: Analyze heap operations
Each engineer is pushed into a min-heap of size at most k, so each push/pop is O(log k). Since k ≤ n, heap operations total O(n log k).Step 3: Combine complexities
Total time is O(n log n + n log k). Since k ≤ n, this simplifies to O(n log n).Final Answer:
Option A -> Option AQuick Check:
Sorting dominates, heap operations are logarithmic in k [OK]
- Confusing heap size with n
- Assuming heap ops are O(k) not O(log k)
- Ignoring sorting cost
Solution
Step 1: Analyze heap operations
Building two heaps of size n takes O(n) each, but heap push/pop operations during processing take O(log n) each.Step 2: Count total operations
Each interval is pushed and popped at most once from each heap, resulting in O(n log n) total time.Final Answer:
Option A -> Option AQuick Check:
Heap operations dominate complexity, confirming O(n log n) [OK]
- Assuming linear time due to single pass
- Confusing n and k in complexity
- Mistaking nested loops for heap operations
