Practice
K sorted arrays and need to find the median of all elements combined without fully merging them. Which algorithmic approach guarantees an optimal solution in terms of time complexity for large inputs?Solution
Step 1: Understand problem constraints
The problem requires finding the median without merging all arrays fully, which is costly for large inputs.Step 2: Identify optimal approach
Binary search on the value range combined with counting elements ≤ mid leverages sorted arrays and avoids full merge, achieving better time complexity.Final Answer:
Option B -> Option BQuick Check:
Binary search on value range is known optimal for this problem [OK]
- Assuming full merge is always optimal
- Confusing greedy with binary search
- Misapplying DP to median finding
K = 2 and jobs = [[0,5],[1,2],[5,3]], what is the final returned list indicating the machine assigned to each job (in original job order)?Solution
Step 1: Sort jobs by start time
Jobs sorted by start time: (0,[0,5]), (1,[1,2]), (2,[5,3]) with indices 0,1,2 respectively.Step 2: Simulate assignment
Initially, available machines = [0,1]. - Job 0 starts at 0: assign machine 0, finish at 5. - Job 1 starts at 1: machine 1 is free, assign machine 1, finish at 3. - Job 2 starts at 5: running machines finish times are 3 and 5; both ≤ 5, so both machines become available. Assign machine 0 (smallest index). Result array indexed by original job indices: [0,1,0].Final Answer:
Option A -> Option AQuick Check:
Trace matches output [0,1,0] [OK]
- Assigning machine 1 to job 1 instead of machine 0
- Not freeing machines before assigning next job
- Mixing up job indices after sorting
lists[0] after the first iteration of the outer while loop when input lists are:List 0: 1 -> 4 -> 5
List 1: 1 -> 3 -> 4
List 2: 2 -> 6
Solution
Step 1: Trace first iteration of outer while loop
Interval = 1, merge lists[0] and lists[1]: merge (1->4->5) and (1->3->4).Step 2: Result of mergeTwoLists(lists[0], lists[1])
Merged list is 1 -> 1 -> 3 -> 4 -> 4 -> 5. lists[2] remains unchanged.Final Answer:
Option A -> Option AQuick Check:
Only first two lists merged in first iteration [OK]
- Merging all lists at once instead of pairs
- Confusing interval doubling
- Assuming full merge after first iteration
arr = [4, 3, 2] and k = 2, what is the value of arr after the function completes?Solution
Step 1: Initialize heap with first k+1 elements
Heap = [4, 3, 2] -> heapify -> [2, 3, 4]Step 2: Since array length is 3 and k+1=3, no for-loop iterations occur; pop all from heap into arr
Pop 2 -> arr[0]=2, pop 3 -> arr[1]=3, pop 4 -> arr[2]=4Final Answer:
Option A -> Option AQuick Check:
Heap pops in ascending order produce sorted array [2,3,4] [OK]
- Forgetting heapify order
- Misindexing target_index
- Assuming for-loop runs when array length ≤ k+1
Solution
Step 1: Understand the variant allowing duplicates
Allowing reuse means duplicates are valid and should be generated and processed multiple times.Step 2: Modify the algorithm accordingly
Removing the 'seen' set allows duplicates to be pushed and popped, correctly reflecting the new problem constraints.Step 3: Consider efficiency
Though duplicates increase heap size, this is necessary to correctly generate all numbers under the new rules.Final Answer:
Option A -> Option AQuick Check:
Removing 'seen' allows duplicates as required [OK]
- Keeping 'seen' and missing duplicates
- Resetting 'seen' incorrectly
- Using counters unnecessarily
