Practice
arrays = [[1,3,5], [2,4,6], [0,7,8]], what is the value of median1 after the binary search loop completes?Solution
Step 1: Calculate total length and median indices
Total length = 3 + 3 + 3 = 9, so mid1 = (9-1)//2 = 4.Step 2: Perform binary search on value range [0,8]
Counting elements ≤ mid at each step narrows down to the smallest number with count > 4, which is 4.Final Answer:
Option D -> Option DQuick Check:
Median of combined sorted array [0,1,2,3,4,5,6,7,8] is 4 [OK]
- Off-by-one in median index
- Miscounting elements ≤ mid
- Confusing median1 and median2
Solution
Step 1: Understand problem constraints
Brute force (generating all sums) is too slow for large inputs.Step 2: Identify efficient approach
Binary search on sum range with counting pairs ≤ mid efficiently narrows down the k-th smallest sum.Final Answer:
Option A -> Option AQuick Check:
Binary search reduces search space logarithmically [OK]
- Assuming greedy approach always works
- Using DP which is inefficient here
K identical machines and a list of jobs, each with a start time and duration. You want to assign each job to a machine so that each machine processes jobs sequentially without overlap, always choosing the earliest available machine with the smallest index. Which algorithmic approach guarantees an optimal assignment minimizing waiting times and correctly handles job scheduling in chronological order?Solution
Step 1: Understand the problem constraints
The problem requires assigning jobs to machines so that each job starts no earlier than its start time, machines process jobs sequentially, and the earliest available machine with the smallest index is chosen.Step 2: Identify the correct approach
Using a min-heap to track machine availability by next free time allows efficient retrieval of the earliest available machine. Sorting jobs by start time ensures chronological processing. This approach guarantees optimal scheduling and respects machine indices.Final Answer:
Option B -> Option BQuick Check:
Min-heap + sorting jobs by start time -> optimal scheduling [OK]
- Using linear search for machine availability leads to O(nK) time
- Sorting by duration ignores job start times and causes conflicts
- DP approach is infeasible due to large state space
[[1,4,5],[1,3,4],[2,6]], what is the value of merged_arrays after the first iteration of the while loop inside merge_k_sorted_arrays?Solution
Step 1: Trace merging pairs in first iteration
Arrays are [[1,4,5],[1,3,4],[2,6]]. Merge first two: merge_two_arrays([1,4,5],[1,3,4]) -> [1,1,3,4,4,5]. The last array [2,6] remains as is because no pair.Step 2: Form merged_arrays after first iteration
merged_arrays = [[1,1,3,4,4,5],[2,6]]Final Answer:
Option D -> Option DQuick Check:
First pair merged correctly, last array unchanged -> matches [[1,1,3,4,4,5],[2,6]] [OK]
- Merging last array incorrectly
- Off-by-one in loop
- Appending arrays instead of merging
nums2 can be reused multiple times in pairs (i.e., pairs can reuse the same element from nums2 multiple times). Which modification to the min-heap approach correctly handles this variant?Solution
Step 1: Understand reuse requirement
Allowing reuse of nums2 elements means after using nums2[j], we can also try nums2[j+1] and nums2[j] again with next nums1 element.Step 2: Modify heap push logic
After popping (i, j), pushing (i, j+1) continues along nums2, and pushing (i+1, j) allows pairing next nums1 element with the same nums2[j], enabling reuse.Step 3: Reject other options
Push all pairs (nums1[i] + nums2[j]) for all i, j into the heap initially and pop k times. is brute force and inefficient. After popping a pair (i, j), push (i, j) again to allow reuse of nums2[j]. causes infinite loops by pushing same pair repeatedly. Modify the heap to store sums with counts of usage and only push new pairs when usage count is less than k. is unnecessarily complex and not standard.Final Answer:
Option B -> Option BQuick Check:
After popping a pair (i, j), push (i, j+1) and also push (i+1, j) if within bounds, allowing reuse of nums2 elements. correctly expands pairs to allow reuse without infinite loops [OK]
- Pushing same pair repeatedly causing infinite loops
- Brute forcing all pairs initially
- Overcomplicating usage counts
