Practice
1. Consider the following Python code implementing the min-heap approach to find the kth smallest element in a sorted matrix. Given the matrix
[[1,3],[2,4]] and k = 3, what value does the function return?easy
Solution
Step 1: Initialize min-heap with first elements
Push (1,0,0) and (2,1,0) into heap.Step 2: Extract elements until count == k
Pop 1 (count=1), push 3 from row 0; pop 2 (count=2), push 4 from row 1; pop 3 (count=3), return 3.Final Answer:
Option C -> Option CQuick Check:
Third smallest element is 3 [OK]
Hint: Trace heap pops and pushes carefully [OK]
Common Mistakes:
- Off-by-one counting of popped elements
- Returning wrong popped value
- Confusing row and column indices
2. Consider the following Python code that finds the k-th smallest number in an m x n multiplication table. What is the output when
m = 3, n = 3, and k = 5?easy
Solution
Step 1: Trace countLessEqual for mid=4
For mid=4, count = sum of min(3, 4//i): i=1 -> min(3,4)=3; i=2 -> min(3,2)=2; i=3 -> min(3,1)=1; total=6 ≥ k=5, so high=4.Step 2: Trace countLessEqual for mid=3
For mid=3, count = i=1 -> 3; i=2 -> 1; i=3 -> 1; total=5 ≥ k=5, so high=3.Step 3: Trace countLessEqual for mid=2
For mid=2, count = i=1 -> 2; i=2 -> 1; i=3 -> 0; total=3 < k=5, so low=3.Final Answer:
Option D -> Option DQuick Check:
Output matches expected k-th smallest number 5 [OK]
Hint: Count function caps counts per row [OK]
Common Mistakes:
- Off-by-one in counting
- Not breaking early in countLessEqual
3. You are given an array of non-negative integers and a number
m. You want to split the array into m non-empty continuous subarrays such that the largest sum among these subarrays is minimized. Which algorithmic approach guarantees finding this minimal largest sum efficiently?easy
Solution
Step 1: Understand the problem constraints
The problem requires minimizing the largest sum amongmsubarrays, which is a classic optimization problem over a numeric answer space.Step 2: Identify the approach that efficiently searches the answer space
Binary search on the possible sums combined with a greedy check to verify feasibility is the standard approach that guarantees optimality and efficiency.Final Answer:
Option C -> Option CQuick Check:
Binary search on answer space is the canonical pattern for this problem [OK]
Hint: Minimize max sum -> binary search on answer space [OK]
Common Mistakes:
- Assuming greedy splitting always yields optimal solution
- Trying DP without pruning leads to exponential time
- Sorting does not help because order matters
4. The following code attempts to find the kth positive missing number from a sorted array
arr. Which line contains a subtle bug that causes incorrect results for some inputs?
def findKthPositive(arr, k):
n = len(arr)
total_missing = arr[-1] - n
if k > total_missing:
return arr[-1] + (k - total_missing)
left, right = 0, n - 1
while left <= right:
mid = (left + right) // 2
missing = arr[mid] - mid # Bug here
if missing < k:
left = mid + 1
else:
right = mid - 1
return arr[right] + (k - (arr[right] - (right + 1)))
medium
Solution
Step 1: Identify the missing count formula
The correct missing count at index mid is arr[mid] - (mid + 1), since indices are zero-based but numbers start at 1.Step 2: Analyze the bug impact
Using arr[mid] - mid undercounts missing numbers by 1, causing incorrect binary search decisions and wrong final result.Final Answer:
Option D -> Option DQuick Check:
Correct missing count formula is arr[mid] - (mid + 1) [OK]
Hint: Missing count must subtract (mid + 1), not mid [OK]
Common Mistakes:
- Off-by-one in missing count calculation
- Incorrect binary search boundary updates
- Ignoring missing numbers before first element
5. What is the time complexity of the recursive modified binary search algorithm for finding a peak element in an array of size n, and why might some candidates mistakenly think it is worse?
medium
Solution
Step 1: Analyze recursive calls
Each call halves the search space, so number of calls is O(log n).Step 2: Confirm work per call
Each call does O(1) work (simple comparisons), so total time is O(log n).Final Answer:
Option B -> Option BQuick Check:
Halving search space leads to logarithmic time [OK]
Hint: Halving input size -> O(log n) time [OK]
Common Mistakes:
- Confusing recursion stack space with time
- Assuming linear scan inside recursion
- Thinking recursion multiplies complexity
