A peak element is an element that is greater than its neighbors. For the first or last element, only one neighbor is considered.
Click to reveal answer
intermediate
Why is binary search suitable for finding a peak element?
Because if the middle element is not a peak, we can decide which half to search next based on the slope (neighbor comparison), reducing the search space efficiently.
Click to reveal answer
intermediate
In the binary search for peak element, what does it mean if nums[mid] < nums[mid + 1]?
It means the peak lies to the right of mid, so we move the search to the right half.
Click to reveal answer
beginner
What is the time complexity of finding a peak element using binary search?
O(log n), where n is the number of elements in the array.
Click to reveal answer
intermediate
Can there be multiple peak elements in an array? If yes, does the binary search always find the first peak?
Yes, there can be multiple peaks. The binary search finds any one peak, not necessarily the first.
Click to reveal answer
What condition indicates that the peak is on the right side during binary search?
Anums[mid] < nums[mid - 1]
Bnums[mid] < nums[mid + 1]
Cnums[mid] == nums[mid + 1]
Dnums[mid] > nums[mid + 1]
✗ Incorrect
If nums[mid] is less than nums[mid + 1], the slope goes up to the right, so the peak must be on the right side.
What is the base case for stopping the binary search when finding a peak element?
AWhen nums[mid] is greater than neighbors
BWhen low > high
CWhen low == high
DWhen mid == 0
✗ Incorrect
We stop when nums[mid] is greater than both neighbors, meaning we found a peak.
What is the time complexity of the peak element binary search algorithm?
AO(1)
BO(n)
CO(n log n)
DO(log n)
✗ Incorrect
Binary search reduces the search space by half each time, so it runs in O(log n).
If the array is strictly increasing, where is the peak element?
AAt the start
BIn the middle
CAt the end
DNo peak exists
✗ Incorrect
In a strictly increasing array, the last element is the peak because it has no right neighbor and is greater than the left.
Can the binary search for peak element be used on unsorted arrays?
AYes, because peak definition depends only on neighbors
BNo, never
CYes, always
DOnly if array is sorted
✗ Incorrect
The algorithm works because peak depends on neighbors, not full sorting.
Explain how binary search helps find a peak element in an array.
Think about how the middle element's neighbors guide the search direction.
You got /4 concepts.
Describe the conditions that determine if an element is a peak in an array.
Consider edge elements and how many neighbors they have.
You got /4 concepts.
Practice
(1/5)
1. What is a peak element in an array when using binary search to find it?
easy
A. An element that is greater than its neighbors
B. The smallest element in the array
C. The first element of the array
D. An element equal to its neighbors
Solution
Step 1: Understand the definition of a peak element
A peak element is one that is strictly greater than its neighbors on both sides (or just one side if at the boundary).
Step 2: Apply this to binary search context
Binary search finds such an element by comparing mid element with neighbors to decide the search direction.
Final Answer:
An element that is greater than its neighbors -> Option A
Quick Check:
Peak element = greater than neighbors [OK]
Hint: Peak element is bigger than neighbors, not smallest [OK]
Common Mistakes:
Confusing peak with minimum element
Thinking peak must be at array ends only
Assuming peak equals neighbors
2. Which of the following is the correct syntax to calculate the middle index in binary search in C?
easy
A. int mid = (high - low) * 2;
B. int mid = (low + high) / 2;
C. int mid = (low - high) / 2;
D. int mid = low + (high - low) / 2;
Solution
Step 1: Understand integer overflow risk in mid calculation
Using (low + high) / 2 can overflow if low and high are large integers.
Step 2: Use safer formula to avoid overflow
Calculate mid as low + (high - low) / 2 to prevent overflow.
Hint: Use low + (high - low)/2 to avoid overflow [OK]
Common Mistakes:
Using (low + high)/2 causing overflow
Subtracting incorrectly in mid calculation
Multiplying instead of dividing
3. What will be the output peak element index for the array [1, 3, 20, 4, 1, 0] using binary search peak finding?
medium
A. 1
B. 2
C. 3
D. 0
Solution
Step 1: Identify peak candidates in the array
Array: [1, 3, 20, 4, 1, 0]. Element 20 at index 2 is greater than neighbors 3 and 4.
Step 2: Binary search will find index 2 as peak
Binary search compares mid elements and moves towards the peak, landing on index 2.
Final Answer:
2 -> Option B
Quick Check:
Peak element index = 2 [OK]
Hint: Peak is local max, here 20 at index 2 [OK]
Common Mistakes:
Choosing first or last element incorrectly
Confusing index with value
Ignoring neighbors in peak definition
4. Consider this code snippet to find a peak element index:
int findPeak(int arr[], int n) {
int low = 0, high = n - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] < arr[mid + 1])
low = mid + 1;
else
high = mid - 1;
}
return low;
}
What is the bug in this code?
medium
A. It should return high instead of low
B. The while condition should be low <= high
C. The else branch should set high = mid, not mid - 1
D. The mid calculation causes overflow
Solution
Step 1: Analyze the else branch update of high
Setting high = mid - 1 skips the mid element which could be a peak.
Step 2: Correct update to include mid in search
Change to high = mid to keep mid in the search space for peak.
Final Answer:
The else branch should set high = mid, not mid - 1 -> Option C
Quick Check:
Keep mid in search by high = mid [OK]
Hint: Use high = mid to avoid skipping potential peak [OK]
Common Mistakes:
Reducing high too early and missing peak
Changing while condition unnecessarily
Ignoring mid element in binary search
5. Given an array [10, 20, 15, 2, 23, 90, 67], how many peak elements can binary search find and which index will it return if it always moves towards the right when equal?
hard
A. 2 peaks, returns index 5
B. 3 peaks, returns index 1
C. 1 peak, returns index 6
D. 2 peaks, returns index 1
Solution
Step 1: Identify all peak elements in the array
Array: [10, 20, 15, 2, 23, 90, 67]. Peaks are 20 (index 1) and 90 (index 5) as both are greater than neighbors.
Step 2: Understand binary search behavior moving right on equal
Binary search will move towards the right peak at index 5 because it moves right when equal or increasing.
Final Answer:
2 peaks, returns index 5 -> Option A
Quick Check:
Two peaks, rightmost peak index 5 [OK]
Hint: Multiple peaks possible; binary search returns one found by direction [OK]
Common Mistakes:
Assuming only one peak always
Ignoring binary search direction rules
Confusing peak count with returned index
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.