Challenge - 5 Problems
Peak Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find the peak element index in the array
What is the output of the following JavaScript code that finds a peak element index using binary search?
DSA Javascript
function findPeakElement(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
console.log(findPeakElement([1, 3, 20, 4, 1, 0]));Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the peak by comparing mid and mid+1 elements.
✗ Incorrect
The function uses binary search to find a peak element index. It compares nums[mid] and nums[mid+1]. If nums[mid] is greater, the peak lies on the left side including mid, so right moves to mid. Otherwise, the peak lies on the right side excluding mid, so left moves to mid+1. Eventually, left equals right at the peak index.
❓ Predict Output
intermediate2:00remaining
Output of peak element finder on a strictly increasing array
What will be the output of the peak element finder function when the input array is strictly increasing?
DSA Javascript
function findPeakElement(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
console.log(findPeakElement([1, 2, 3, 4, 5]));Attempts:
2 left
💡 Hint
In a strictly increasing array, the last element is the peak.
✗ Incorrect
Since the array is strictly increasing, each element is smaller than the next. The binary search will keep moving left to mid+1 until it reaches the last index, which is the peak.
🔧 Debug
advanced2:00remaining
Identify the error in the peak element finder code
What error will the following code produce when run, and why?
DSA Javascript
function findPeakElement(nums) {
let left = 0, right = nums.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[mid + 1]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
console.log(findPeakElement([1, 2, 3, 1]));Attempts:
2 left
💡 Hint
Check the loop condition and the index used inside the loop.
✗ Incorrect
The loop condition 'left <= right' allows mid to reach the last index. Then nums[mid + 1] accesses out of bounds causing a TypeError.
🧠 Conceptual
advanced2:00remaining
Why binary search works for finding a peak element
Why does the binary search approach correctly find a peak element in an unsorted array where adjacent elements are not equal?
Attempts:
2 left
💡 Hint
Think about the slope direction at mid compared to mid+1.
✗ Incorrect
If nums[mid] > nums[mid+1], a peak must exist on the left side including mid. Otherwise, a peak exists on the right side. This property allows binary search to discard half the array each step.
❓ Predict Output
expert2:00remaining
Output of peak element finder on array with multiple peaks
What is the output index of the peak element finder function when the input array has multiple peaks?
DSA Javascript
function findPeakElement(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
console.log(findPeakElement([1, 3, 2, 4, 1, 0]));Attempts:
2 left
💡 Hint
The function returns one peak index, not necessarily the first or last peak.
✗ Incorrect
The binary search narrows down to one peak. In this array, indices 1 and 3 are peaks. The algorithm returns index 3 because of how it compares mid and mid+1.