Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Peak Element 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 C++
int findPeakElement(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[mid + 1]) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}

vector<int> nums = {1, 3, 20, 4, 1, 0};
int peakIndex = findPeakElement(nums);
cout << peakIndex;
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Check where the element is greater than its next neighbor.
❓ 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 approach?
DSA C++
int findPeakElement(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[mid + 1]) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}

vector<int> nums = {1, 2, 3, 1, 2, 1};
int peakIndex = findPeakElement(nums);
cout << peakIndex;
A2
B4
C1
D3
Attempts:
2 left
💡 Hint
Binary search finds any peak, not necessarily the first or last.
🧠 Conceptual
advanced
2:00remaining
Why Binary Search Works for Peak Element?
Why does the binary search approach always find a peak element in an unsorted array?
ABecause if the middle element is smaller than the next, a peak must exist on the right side; otherwise, on the left side.
BBecause the array is sorted, so the middle element is always a peak.
CBecause the algorithm checks all elements one by one to find the peak.
DBecause the peak element is always at the start or end of the array.
Attempts:
2 left
💡 Hint
Think about the direction where the array is increasing or decreasing.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Peak Element Code
What error will this code cause when finding a peak element?
DSA C++
int findPeakElement(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[mid + 1]) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return left;
}
ACompiles but returns -1 always
BRuntime error due to out-of-bounds access at nums[mid + 1]
CReturns wrong peak index because of incorrect mid calculation
DInfinite loop because left and right pointers never converge
Attempts:
2 left
💡 Hint
Check the condition 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?
ASort the array first then pick the middle element as peak
BLinear scan from start to end to find any element greater than neighbors
CUse binary search but ignore wrap-around and compare only nums[mid] and nums[mid + 1]
DUse the standard binary search comparing nums[mid] and nums[(mid + 1) % n] to handle wrap-around
Attempts:
2 left
💡 Hint
Think about how to handle the circular nature in index calculation.

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. An element that is smaller than its neighbors
C. The first element of the array
D. The last element of the array

Solution

  1. 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 only one side if at the boundary).
  2. Step 2: Apply this to binary search context

    Binary search finds such an element by comparing mid element with neighbors to decide search direction.
  3. Final Answer:

    An element that is greater than its neighbors -> Option A
  4. Quick Check:

    Peak element = greater than neighbors [OK]
Hint: Peak element is bigger than neighbors, not smaller [OK]
Common Mistakes:
  • Confusing peak with smallest element
  • Thinking peak is always first or last element
  • Ignoring boundary conditions
2. Which of the following is the correct syntax to start binary search for peak element in C++?
easy
A. int left = 1, right = n;
B. int left = 0, right = n;
C. int left = 0, right = n - 1;
D. int left = 1, right = n - 1;

Solution

  1. Step 1: Identify valid index range for array of size n

    Array indices go from 0 to n-1, so left should start at 0 and right at n-1.
  2. Step 2: Check each option for correct initialization

    int left = 0, right = n - 1; correctly initializes left=0 and right=n-1, suitable for binary search.
  3. Final Answer:

    int left = 0, right = n - 1; -> Option C
  4. Quick Check:

    Index range = 0 to n-1 [OK]
Hint: Array indices start at 0, end at n-1 [OK]
Common Mistakes:
  • Starting left at 1 instead of 0
  • Setting right to n instead of n-1
  • Confusing inclusive and exclusive ranges
3. What will be the output index of the peak element for the array [1, 3, 20, 4, 1, 0] using binary search peak finding?
medium
A. 0
B. 2
C. 3
D. 1

Solution

  1. Step 1: Identify peak elements in the array

    Array: [1, 3, 20, 4, 1, 0]. 20 at index 2 is greater than neighbors 3 and 4, so it's a peak.
  2. Step 2: Binary search will find index 2 as peak

    Binary search compares mid elements and moves left or right until it finds 20 at index 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Peak at index 2 = 20 [OK]
Hint: Peak is local max, here 20 at index 2 [OK]
Common Mistakes:
  • Choosing first or last element as peak incorrectly
  • Confusing index with value
  • Not checking both neighbors
4. Given the following code snippet, what is the bug that prevents finding the peak element correctly?
int findPeak(int arr[], int n) {
  int left = 0, right = n - 1;
  while (left < right) {
    int mid = (left + right) / 2;
    if (arr[mid] < arr[mid + 1])
      left = mid + 1;
    else
      right = mid - 1;
  }
  return left;
}
medium
A. Decreasing right by 1 skips potential peak
B. Mid calculation causes overflow
C. Loop condition should be left <= right
D. Returning left instead of right

Solution

  1. Step 1: Analyze the binary search update steps

    If arr[mid] >= arr[mid+1], the code sets right = mid - 1, which skips mid as a candidate peak.
  2. Step 2: Correct approach is to set right = mid to include mid in search

    Setting right = mid keeps mid in range; subtracting 1 may miss the peak.
  3. Final Answer:

    Decreasing right by 1 skips potential peak -> Option A
  4. Quick Check:

    Right update must keep mid included [OK]
Hint: Do not exclude mid when moving right pointer [OK]
Common Mistakes:
  • Setting right = mid - 1 instead of right = mid
  • Ignoring mid element as possible peak
  • Incorrect loop condition
5. You have an array [10, 20, 15, 2, 23, 90, 67]. Using binary search for peak element, which index will be returned and why?
hard
A. 0, because 10 is the first element
B. 1, because 20 is a peak element
C. 6, because 67 is the last element
D. 5, because 90 is a peak element

Solution

  1. 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). 90 is greater than neighbors 23 and 67.
  2. Step 2: Binary search tends to find a peak, often the higher one

    Binary search will find 90 at index 5 because it is a higher peak than 20 at index 1.
  3. Final Answer:

    5, because 90 is a peak element -> Option D
  4. Quick Check:

    Highest peak found = index 5 [OK]
Hint: Binary search finds any peak; higher peaks likely found first [OK]
Common Mistakes:
  • Assuming first peak is always returned
  • Ignoring multiple peaks in array
  • Confusing last element as peak without checking neighbors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard