Practice
arr. What is the returned value when calling findKthPositive([2, 4, 7], 3)?
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 + 1)
if missing < k:
left = mid + 1
else:
right = mid - 1
return arr[right] + (k - (arr[right] - (right + 1)))
Solution
Step 1: Calculate total missing numbers
Array length n=3, arr[-1]=7, total_missing = 7 - 3 = 4. Since k=3 ≤ 4, proceed with binary search.Step 2: Perform binary search iterations
Initial left=0, right=2. - mid=1, missing=arr[1] - (1+1) = 4 - 2 = 2 < 3 -> left=2 - mid=2, missing=arr[2] - (2+1) = 7 - 3 = 4 ≥ 3 -> right=1 Loop ends as left=2 > right=1. Return arr[right] + (k - (arr[right] - (right + 1))) = arr[1] + (3 - (4 - 2)) = 4 + (3 - 2) = 5.Final Answer:
Option A -> Option AQuick Check:
Binary search narrows to index 1, final missing number is 5 [OK]
- Off-by-one in missing calculation
- Returning arr[left] instead of arr[right]
- Miscomputing missing count as arr[mid] - mid
target = 2 in nums = [1, 3, 5, 6]. What is the final returned value?Solution
Step 1: Trace binary_search(0, 3)
mid = (0+3)//2 = 1, nums[1] = 3 > 2, recurse left: binary_search(0, 0)Step 2: Trace binary_search(0, 0)
mid = (0+0)//2 = 0, nums[0] = 1 < 2, recurse right: binary_search(1, 0)Step 3: Base case binary_search(1, 0)
left > right, return left = 1 as insertion indexFinal Answer:
Option B -> Option BQuick Check:
Target 2 fits between 1 and 3 at index 1 [OK]
- Returning right instead of left at base case
- Off-by-one in mid calculation
- Confusing left and right updates
def findPeakElement(nums):
def binarySearch(low, high):
if low == high:
return low
mid = (low + high) // 2
if nums[mid] < nums[mid]: # Line X
return binarySearch(mid + 1, high)
else:
return binarySearch(low, mid)
return binarySearch(0, len(nums) - 1)
Solution
Step 1: Identify suspicious comparison
Line 5 compares nums[mid] with itself, which is always false, causing wrong recursion path.Step 2: Understand impact
This bug causes infinite recursion or incorrect peak detection because the condition never triggers correctly.Final Answer:
Option A -> Option AQuick Check:
Comparison must be nums[mid] < nums[mid + 1] [OK]
- Comparing mid with mid instead of mid+1
- Off-by-one causing infinite recursion
- Returning wrong index after recursion
n x n sorted matrix, where k ≤ n²? Assume the heap operations take O(log n) time.Solution
Step 1: Analyze heap size and operations
The heap holds at most n elements (one from each row), so heap operations cost O(log n).Step 2: Count total heap operations
We pop and push at most k times, so total time is O(k log n).Final Answer:
Option A -> Option AQuick Check:
Heap size bounded by n, operations proportional to k [OK]
- Assuming heap size grows to k or n²
- Confusing k and n in log factors
- Thinking complexity is O(n^2 log n)
Solution
Step 1: Analyze loop control variables
The variablestartcontrols the loop progress and must be updated to avoid infinite loops.Step 2: Identify missing update
Line 7 lacksstart = end, sostartnever advances, causing infinite loop or wrong split counts.Final Answer:
Option A -> Option AQuick Check:
Without updatingstart, loop never progresses [OK]
- Forgetting to update loop index after binary search
- Misusing bisect boundaries causing off-by-one errors
- Incorrect initialization of count or start
