0
0
DSA Goprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Peak Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find Peak Element Output
What is the output index of the peak element for the given array using the binary search approach?
DSA Go
package main

import "fmt"

func findPeakElement(nums []int) int {
    left, right := 0, len(nums)-1
    for left < right {
        mid := (left + right) / 2
        if nums[mid] > nums[mid+1] {
            right = mid
        } else {
            left = mid + 1
        }
    }
    return left
}

func main() {
    nums := []int{1, 3, 20, 4, 1, 0}
    peakIndex := findPeakElement(nums)
    fmt.Println(peakIndex)
}
A2
B1
C3
D5
Attempts:
2 left
💡 Hint
Check where the array value is greater than its next neighbor to decide which side to search.
Predict Output
intermediate
2:00remaining
Find Peak Element Output with Multiple Peaks
What is the output index of the peak element for the array with multiple peaks using the binary search method?
DSA Go
package main

import "fmt"

func findPeakElement(nums []int) int {
    left, right := 0, len(nums)-1
    for left < right {
        mid := (left + right) / 2
        if nums[mid] > nums[mid+1] {
            right = mid
        } else {
            left = mid + 1
        }
    }
    return left
}

func main() {
    nums := []int{1, 2, 1, 3, 5, 6, 4}
    peakIndex := findPeakElement(nums)
    fmt.Println(peakIndex)
}
A5
B1
C4
D3
Attempts:
2 left
💡 Hint
The function returns any peak, not necessarily the first peak.
🧠 Conceptual
advanced
1:30remaining
Why Binary Search Works for Finding a Peak Element
Why does the binary search approach guarantee finding a peak element in an unsorted array?
ABecause the binary search checks all elements one by one to find the peak.
BBecause the array is sorted, so the middle element is always a peak.
CBecause if nums[mid] > nums[mid+1], a peak must exist on the left side including mid.
DBecause the peak is always at the start or end of the array.
Attempts:
2 left
💡 Hint
Think about how comparing mid and mid+1 helps decide which side to search.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Peak Element Binary Search
What error does the following code produce when run, and why?
DSA Go
package main

import "fmt"

func findPeakElement(nums []int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := (left + right) / 2
        if nums[mid] > nums[mid+1] {
            right = mid - 1
        } else {
            left = mid + 1
        }
    }
    return left
}

func main() {
    nums := []int{1, 2, 3, 1}
    peakIndex := findPeakElement(nums)
    fmt.Println(peakIndex)
}
ACompiles but prints -1
BInfinite loop
CReturns incorrect peak index
DIndex out of range runtime error
Attempts:
2 left
💡 Hint
Check the condition nums[mid+1] when mid is at the last index.
🚀 Application
expert
3:00remaining
Find Peak Element in Circular Array
Given a circular array where the end connects to the start, which approach correctly finds a peak element index?
AUse linear scan to check each element against neighbors ignoring circularity.
BModify binary search to compare nums[mid] with nums[(mid+1) % n] and nums[(mid-1+n) % n] to handle circular neighbors.
CApply standard binary search without changes since circularity doesn't affect peak definition.
DSort the array first then find the maximum element index.
Attempts:
2 left
💡 Hint
Think about how to access neighbors in a circular array safely.