0
0
DSA Javascriptprogramming~10 mins

Find Peak Element Using Binary Search in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the binary search with the correct initial left index.

DSA Javascript
function findPeakElement(nums) {
  let left = [1];
  let right = nums.length - 1;
  while (left < right) {
    // logic here
  }
  return left;
}
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dnums.length
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 or negative index causes skipping elements or errors.
2fill in blank
medium

Complete the code to calculate the middle index correctly.

DSA Javascript
while (left < right) {
  let mid = Math.floor((left [1] right) / 2);
  // logic here
}
Drag options to blanks, or click blank then click option'
A*
B-
C+
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition causes wrong mid calculation.
3fill in blank
hard

Fix the error in the condition to decide which side to search next.

DSA Javascript
if (nums[mid] [1] nums[mid + 1]) {
  left = mid + 1;
} else {
  right = mid;
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < reverses the search direction and breaks the logic.
4fill in blank
hard

Fill both blanks to complete the binary search loop and return the peak index.

DSA Javascript
while ([1] < [2]) {
  let mid = Math.floor((left + right) / 2);
  if (nums[mid] < nums[mid + 1]) {
    left = mid + 1;
  } else {
    right = mid;
  }
}
return left;
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cmid
Dnums.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid or nums.length in the loop condition causes infinite loops or errors.
5fill in blank
hard

Fill all three blanks to create a complete function that finds a peak element index.

DSA Javascript
function findPeakElement(nums) {
  let [1] = 0;
  let [2] = nums.length - 1;
  while (left < right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] [3] nums[mid + 1]) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  return left;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up left and right variable names or using wrong comparison operators.