Challenge - 5 Problems
Peak Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the peak element index in the array
What is the output index of the peak element for the given array using binary search?
DSA Python
def find_peak(nums): left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[mid + 1]: right = mid else: left = mid + 1 return left print(find_peak([1, 3, 20, 4, 1, 0]))
Attempts:
2 left
💡 Hint
Think about how the binary search moves left or right based on the comparison between mid and mid+1.
✗ Incorrect
The function finds the peak by comparing mid element with its next element. If mid is greater, the peak is on the left side including mid; else on the right side excluding mid. The peak index is 2 where value is 20.
❓ Predict Output
intermediateOutput of peak element index for strictly increasing array
What index does the binary search peak finder return for a strictly increasing array?
DSA Python
def find_peak(nums): left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[mid + 1]: right = mid else: left = mid + 1 return left print(find_peak([1, 2, 3, 4, 5, 6]))
Attempts:
2 left
💡 Hint
In a strictly increasing array, the peak is at the last element.
✗ Incorrect
Since the array is strictly increasing, the peak is the last element at index 5. The binary search moves right until it reaches the end.
❓ Predict Output
advancedFind peak element index in array with multiple peaks
What is the output index of the peak element for the array with multiple peaks?
DSA Python
def find_peak(nums): left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[mid + 1]: right = mid else: left = mid + 1 return left print(find_peak([1, 3, 5, 2, 4, 6, 1]))
Attempts:
2 left
💡 Hint
The function returns one peak index, not necessarily the first peak.
✗ Incorrect
The binary search finds a peak at index 5 where value is 6. Other peaks exist but the algorithm returns one valid peak.
🧠 Conceptual
advancedWhy binary search works for finding a peak element
Why does the binary search approach guarantee finding a peak element in an array?
Attempts:
2 left
💡 Hint
Think about the comparison between mid and mid+1 and what it implies about the slope.
✗ Incorrect
If nums[mid] > nums[mid+1], a peak must exist on the left side including mid; else on the right side. This property ensures binary search finds a peak.
🔧 Debug
expertIdentify the error in the peak element binary search code
What error does the following code produce when run, and why?
DSA Python
def find_peak(nums): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] > nums[mid + 1]: right = mid - 1 else: left = mid + 1 return left print(find_peak([1, 2, 3, 4, 5, 6]))
Attempts:
2 left
💡 Hint
Check the loop condition and how mid+1 is accessed near the array end.
✗ Incorrect
The loop condition allows mid to be the last index, so nums[mid + 1] causes IndexError. The condition should be left < right to avoid this.
