Hint: Use Math.floor((left + right) / 2) for mid index [OK]
Common Mistakes:
Not using Math.floor causes float index
Using only division without parentheses
Incorrect operator precedence in calculation
3. What will be the output of the following code snippet?
function findPeak(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
console.log(findPeak([1, 3, 2, 4, 1]));
medium
A. 3
B. 1
C. 2
D. 0
Solution
Step 1: Trace the binary search steps on array [1,3,2,4,1]
Initial left=0, right=4. mid=2 (value=2). Compare nums[2]=2 and nums[3]=4. Since 2 < 4, left=mid+1=3.
Step 2: Next iteration with left=3, right=4
mid=3 (value=4). Compare nums[3]=4 and nums[4]=1. Since 4 > 1, right=mid=3.
Step 3: Loop ends as left=right=3
Return left=3, which is index of peak element 4.
Final Answer:
3 -> Option A
Quick Check:
Peak index = 3 [OK]
Hint: Compare mid and mid+1 to decide left or right [OK]
Common Mistakes:
Returning value instead of index
Not updating left or right correctly
Confusing mid calculation
4. Identify the bug in this binary search code to find a peak element:
function findPeak(nums) {
let left = 0, right = nums.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
medium
A. The comparison should be nums[mid] > nums[mid + 1] instead of <.
B. The mid calculation is incorrect.
C. The return statement should return right instead of left.
D. The loop condition should be left < right instead of <=.
Solution
Step 1: Analyze the loop condition
The loop uses left <= right, which can cause mid+1 to go out of bounds when mid = right.
Step 2: Correct loop condition for safe mid+1 access
Changing to left < right ensures mid+1 is always valid inside the loop.
Final Answer:
The loop condition should be left < right instead of <=. -> Option D
Quick Check:
Loop condition left < right avoids out-of-bounds [OK]
Hint: Use left < right to avoid mid+1 overflow [OK]
Common Mistakes:
Using <= causes index out of range error
Changing mid calculation unnecessarily
Returning wrong index
5. Given a strictly increasing then strictly decreasing array nums, how can you modify the binary search to find the peak element efficiently? Choose the best approach.
hard
A. Use binary search comparing nums[mid] and nums[mid-1], move left or right accordingly.
B. Use binary search comparing nums[mid] and nums[mid+1], move left or right accordingly.
C. Scan the array linearly from start to find the maximum element.
D. Sort the array first, then pick the middle element.
Solution
Step 1: Understand the array shape
The array increases then decreases, so the peak is the maximum element where nums[mid] > nums[mid+1].
Step 2: Use binary search comparing nums[mid] and nums[mid+1]
If nums[mid] > nums[mid+1], move right pointer to mid; else move left pointer to mid+1.
Step 3: Avoid linear scan or sorting
Linear scan is O(n), sorting changes order and is unnecessary.
Final Answer:
Use binary search comparing nums[mid] and nums[mid+1], move left or right accordingly. -> Option B
Quick Check:
Compare mid and mid+1 for peak in bitonic array [OK]
Hint: Compare mid and mid+1 to find peak in bitonic array [OK]
Common Mistakes:
Using linear scan instead of binary search
Sorting array destroys peak property
Comparing mid and mid-1 incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.