0
0
DSA Javascriptprogramming~20 mins

Find Peak Element Using Binary Search in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Peak Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]));
A2
B3
C1
D5
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the peak by comparing mid and mid+1 elements.
Predict Output
intermediate
2: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]));
A4
B0
C2
D3
Attempts:
2 left
💡 Hint
In a strictly increasing array, the last element is the peak.
🔧 Debug
advanced
2: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]));
ASyntaxError
BReturns 2 correctly
CTypeError: Cannot read property '1' of undefined
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition and the index used inside the loop.
🧠 Conceptual
advanced
2: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?
ABecause the peak is always at the start or end of the array.
BBecause the array is sorted, so the middle element is always a peak.
CBecause the algorithm checks every element sequentially until it finds a peak.
DBecause at least one side of mid must contain a peak due to the array's property, allowing us to discard half each time.
Attempts:
2 left
💡 Hint
Think about the slope direction at mid compared to mid+1.
Predict Output
expert
2: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]));
A1
B3
C2
D4
Attempts:
2 left
💡 Hint
The function returns one peak index, not necessarily the first or last peak.