0
0
DSA Goprogramming~10 mins

Find Peak Element Using Binary Search in DSA Go - 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 left boundary.

DSA Go
left := [1]
Drag options to blanks, or click blank then click option'
A-1
B0
C1
Dlen(nums)
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 instead of 0
Using negative indices
Setting left to the length of the array
2fill in blank
medium

Complete the code to calculate the middle index correctly.

DSA Go
mid := left + (right - left) [1] 2
Drag options to blanks, or click blank then click option'
A-
B*
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division
Adding instead of dividing
Subtracting instead of dividing
3fill in blank
hard

Fix the error in the condition to check if mid is a peak element.

DSA Go
if nums[mid] > nums[mid+[1]] {
Drag options to blanks, or click blank then click option'
A0
B2
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with nums[mid] itself
Using mid+0 which is the same element
Using mid-1 which is the previous element
4fill in blank
hard

Fill both blanks to update the search boundaries correctly.

DSA Go
if nums[mid] > nums[mid+[1]] {
    right = [2]
} else {
    left = mid + 1
}
Drag options to blanks, or click blank then click option'
A1
Bmid
C+1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting right = mid - 1 instead of mid
Using mid + 0 instead of mid + 1
Not updating boundaries correctly causing infinite loop
5fill in blank
hard

Fill all three blanks to return the peak element index after the loop.

DSA Go
return [1] // The peak element index is at [2] or [3]
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cmid
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Returning mid which may not be updated after loop
Returning 0 which is not necessarily the peak
Returning only right without considering left