Challenge - 5 Problems
Peak Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind Peak Element Output
What is the output of the following C# code that finds a peak element index in the array using binary search?
DSA C#
int[] nums = {1, 3, 20, 4, 1, 0}; int FindPeakElement(int[] arr) { int left = 0, right = arr.Length - 1; while (left < right) { int mid = left + (right - left) / 2; if (arr[mid] > arr[mid + 1]) { right = mid; } else { left = mid + 1; } } return left; } Console.WriteLine(FindPeakElement(nums));
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the peak by comparing mid and mid+1 elements.
✗ Incorrect
The binary search compares elements at mid and mid+1. Since 20 > 4, it moves right to mid, eventually finding index 2 as the peak.
🧠 Conceptual
intermediateUnderstanding Peak Element Conditions
Which condition correctly describes a peak element in an array for the binary search approach?
Attempts:
2 left
💡 Hint
A peak is like a mountain top, higher than what is next to it.
✗ Incorrect
A peak element is greater than its neighbors or is at the boundary where only one neighbor exists.
❓ Predict Output
advancedBinary Search Peak Element Edge Case
What is the output of the code when the input array is strictly increasing?
DSA C#
int[] nums = {1, 2, 3, 4, 5}; int FindPeakElement(int[] arr) { int left = 0, right = arr.Length - 1; while (left < right) { int mid = left + (right - left) / 2; if (arr[mid] > arr[mid + 1]) { right = mid; } else { left = mid + 1; } } return left; } Console.WriteLine(FindPeakElement(nums));
Attempts:
2 left
💡 Hint
In a strictly increasing array, the peak is at the end.
✗ Incorrect
Since each element is smaller than the next, the binary search moves right until the last index, which is the peak.
🔧 Debug
advancedIdentify the Error in Peak Element Code
What error will the following code produce when run?
DSA C#
int[] nums = {1, 2, 3, 4, 5}; int FindPeakElement(int[] arr) { int left = 0, right = arr.Length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] > arr[mid + 1]) { right = mid - 1; } else { left = mid + 1; } } return left; } Console.WriteLine(FindPeakElement(nums));
Attempts:
2 left
💡 Hint
Check the condition when mid+1 is accessed near the array end.
✗ Incorrect
The code can access arr[mid + 1] when mid is at the last index, causing IndexOutOfRangeException.
🚀 Application
expertFind Peak Element in a Large Array
Given a large array where multiple peak elements exist, which index will the binary search method return?
DSA C#
int[] nums = {1, 3, 2, 5, 4, 6, 1}; int FindPeakElement(int[] arr) { int left = 0, right = arr.Length - 1; while (left < right) { int mid = left + (right - left) / 2; if (arr[mid] > arr[mid + 1]) { right = mid; } else { left = mid + 1; } } return left; } Console.WriteLine(FindPeakElement(nums));
Attempts:
2 left
💡 Hint
The binary search finds one peak, not necessarily the highest.
✗ Incorrect
The method returns index 1, a peak found by the binary search process.
