Challenge - 5 Problems
Peak Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Check where the array value is greater than its next neighbor to decide which side to search.
✗ Incorrect
The binary search compares mid element with mid+1 element. Since nums[2] = 20 is greater than nums[3] = 4, the peak lies at or before index 2. The loop narrows down to index 2 as the peak.
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
The function returns any peak, not necessarily the first peak.
✗ Incorrect
The binary search finds index 5 where value 6 is a peak (greater than neighbors 5 and 4).
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Think about how comparing mid and mid+1 helps decide which side to search.
✗ Incorrect
If nums[mid] > nums[mid+1], then a peak exists on the left side including mid because the slope is descending after mid. Otherwise, the peak is on the right side.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Check the condition nums[mid+1] when mid is at the last index.
✗ Incorrect
The loop condition allows mid to be the last index, so nums[mid+1] causes index out of range error.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Think about how to access neighbors in a circular array safely.
✗ Incorrect
In a circular array, neighbors wrap around. Using modulo ensures correct neighbor indices for comparison in binary search.