Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 or negative index causes skipping elements or errors.
✗ Incorrect
The binary search starts from the first index 0 as the left boundary.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition causes wrong mid calculation.
✗ Incorrect
The middle index is calculated by adding left and right, then dividing by 2.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < reverses the search direction and breaks the logic.
✗ Incorrect
If nums[mid] is less than nums[mid + 1], the peak is on the right side.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid or nums.length in the loop condition causes infinite loops or errors.
✗ Incorrect
The loop continues while left is less than right to narrow down the peak.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up left and right variable names or using wrong comparison operators.
✗ Incorrect
Initialize left and right pointers, then compare nums[mid] < nums[mid + 1] to decide direction.