Practice
[1, 3, 2]?
def findPeakElement(nums):
def binarySearch(low, high):
if low == high:
return low
mid = (low + high) // 2
if nums[mid] < nums[mid + 1]:
return binarySearch(mid + 1, high)
else:
return binarySearch(low, mid)
return binarySearch(0, len(nums) - 1)
print(findPeakElement([1, 3, 2]))
Solution
Step 1: Trace first call binarySearch(0, 2)
mid = (0+2)//2 = 1; nums[1]=3, nums[2]=2; since 3 > 2, recurse binarySearch(0,1)Step 2: Trace second call binarySearch(0, 1)
mid = (0+1)//2 = 0; nums[0]=1, nums[1]=3; since 1 < 3, recurse binarySearch(1,1)Step 3: Trace third call binarySearch(1, 1)
low == high, return 1Final Answer:
Option D -> Option DQuick Check:
Returned index 1 corresponds to peak element 3 [OK]
- Returning mid instead of low/high
- Off-by-one in mid calculation
- Confusing which half to recurse
mid and the corresponding row, col during the first iteration of the while loop?Solution
Step 1: Calculate initial boundaries and mid
m=2, n=2, left=0, right=3, so mid=(0+3)//2=1.Step 2: Map mid to 2D indices
row, col = divmod(1, 2) -> row=0, col=1.Final Answer:
Option A -> Option AQuick Check:
mid=1 corresponds to matrix[0][1]=4, correct mapping [OK]
- Off-by-one in mid calculation
- Confusing row and col indices
- Using mid as direct index without divmod
r such that r * r ≤ x for a given non-negative integer x. Which algorithmic approach guarantees an efficient and correct solution for this problem?Solution
Step 1: Understand the problem constraints
The problem requires finding the integer square root efficiently without checking all numbers linearly.Step 2: Identify the suitable algorithm
Modified binary search efficiently narrows down the search space by comparing mid*mid withx, guaranteeing logarithmic time complexity.Final Answer:
Option D -> Option DQuick Check:
Binary search halves the search space each iteration [OK]
- Assuming greedy linear search is efficient
- Confusing DP with this problem
- Using sorting which is irrelevant
Solution
Step 1: Identify recursion depth
Binary search halves the search space each call, so max recursion depth is O(log n).Step 2: Analyze space usage per call
Each call uses constant space; no new arrays are created, so total space is O(log n) for the call stack.Step 3: Clarify space complexity definition
Since no additional data structures are created and only a fixed number of variables are used per call, the extra space excluding call stack is O(1).Step 4: Distinguish between auxiliary space and call stack
Auxiliary space is O(1), but total space including call stack is O(log n).Final Answer:
Option A -> Option AQuick Check:
Recursive calls stack up to log n depth, no extra arrays [OK]
- Assuming O(1) ignoring recursion stack
- Thinking new arrays are created each call
- Confusing time and space complexity
arr can contain negative numbers and zero, and you still need to find the kth positive missing number (positive integers missing from the array). Which of the following changes to the optimal binary search approach is correct?Solution
Step 1: Understand the problem modification
Negative numbers and zero do not affect the count of missing positive integers, but they disrupt the assumption that arr contains only positive sorted numbers.Step 2: Adjust the input for binary search
Filtering out non-positive numbers creates a sorted array of positive integers, restoring the problem's original assumptions and enabling correct binary search.Step 3: Reject other options
No change (B) fails because negative numbers break missing count logic; offset (C) is ambiguous and error-prone; brute force (D) is inefficient and unnecessary.Final Answer:
Option C -> Option CQuick Check:
Filtering preserves correctness and efficiency [OK]
- Assuming binary search works without filtering
- Trying to adjust missing count with offsets
- Switching to brute force unnecessarily
