Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Find Peak Element Using Binary Search in DSA Typescript - 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
Output of Peak Element Finder on a Simple Array
What is the output of the following TypeScript code that finds a peak element index in the array?
DSA Typescript
function findPeakElement(nums: number[]): number {
  let left = 0;
  let right = nums.length - 1;
  while (left < right) {
    const mid = Math.floor((left + right) / 2);
    if (nums[mid] > nums[mid + 1]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
}

const arr = [1, 3, 20, 4, 1, 0];
console.log(findPeakElement(arr));
A3
B2
C1
D5
Attempts:
2 left
💡 Hint
Trace the binary search steps comparing mid and mid+1 elements.
🧠 Conceptual
intermediate
1:30remaining
Understanding Peak Element Definition
Which of the following best describes a peak element in an array?
AAn element that is smaller than its neighbors.
BAn element that is the maximum in the entire array.
CAn element that is greater than its immediate neighbors.
DAn element that appears more than once in the array.
Attempts:
2 left
💡 Hint
Think about local maximums.
🔧 Debug
advanced
2:00remaining
Identify the Error in Peak Element Binary Search
What error will the following TypeScript code produce when run?
DSA Typescript
function findPeak(nums: number[]): number {
  let left = 0;
  let right = nums.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (nums[mid] > nums[mid + 1]) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  return left;
}

console.log(findPeak([1, 2, 3, 4]));
AReturns 3
BReturns 2
CInfinite loop
DRuntime error: Index out of bounds
Attempts:
2 left
💡 Hint
Check the condition when accessing nums[mid + 1].
❓ Predict Output
advanced
2:00remaining
Output of Peak Element Finder on Strictly Increasing Array
What is the output of the following TypeScript code when the input array is strictly increasing?
DSA Typescript
function findPeakElement(nums: number[]): number {
  let left = 0;
  let right = nums.length - 1;
  while (left < right) {
    const mid = Math.floor((left + right) / 2);
    if (nums[mid] > nums[mid + 1]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
}

const arr = [1, 2, 3, 4, 5, 6];
console.log(findPeakElement(arr));
A5
B0
C3
D6
Attempts:
2 left
💡 Hint
In a strictly increasing array, the peak is at the end.
🚀 Application
expert
2:30remaining
Number of Peak Elements in a Given Array
Given the array [1, 3, 2, 4, 1, 0, 5], how many peak elements does it contain?
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Count elements greater than neighbors.

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

  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 just 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 which half to search next.
  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 smallest [OK]
Common Mistakes:
  • Thinking peak is smallest element
  • Confusing peak with first or last element always
  • Assuming peak equals neighbors
2. Which of the following is the correct TypeScript syntax to start a binary search loop for finding a peak element in array nums?
easy
A. while(left < right) { ... }
B. if(nums[mid] > nums[mid+1]) { ... }
C. for(let i = 0; i < nums.length; i++)
D. switch(nums[mid]) { ... }

Solution

  1. Step 1: Identify the loop type for binary search

    Binary search uses a while loop with condition left < right to narrow down the search range.
  2. Step 2: Check options for correct binary search syntax

    while(left < right) { ... } shows the correct while loop structure for binary search.
  3. Final Answer:

    while(left < right) { ... } -> Option A
  4. Quick Check:

    Binary search uses while loop [OK]
Hint: Binary search uses while(left < right) loop [OK]
Common Mistakes:
  • Using for loop instead of while
  • Using if instead of loop for search
  • Using switch incorrectly for binary search
3. What will be the output index of a peak element for the array nums = [1, 3, 20, 4, 1, 0] using binary search peak finding?
medium
A. 3
B. 1
C. 2
D. 0

Solution

  1. Step 1: Identify peak elements in the array

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

    Binary search compares mid with neighbors and narrows to index 2.
  3. Final Answer:

    2 -> Option C
  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 incorrectly
  • Confusing peak with global max always
  • Ignoring neighbors in peak definition
4. Given this TypeScript code snippet to find a peak element, what is the bug?
let left = 0;
let right = nums.length - 1;
while (left < right) {
  let mid = Math.floor((left + right) / 2);
  if (nums[mid] < nums[mid + 1]) {
    left = mid + 1;
  } else {
    right = mid - 1;
  }
}
return left;
medium
A. Return should be right instead of left
B. The loop condition should be while(left <= right)
C. The mid calculation is incorrect
D. The else branch should set right = mid, not mid - 1

Solution

  1. Step 1: Analyze the else branch update of right pointer

    Setting right = mid - 1 skips the mid element, possibly missing peak.
  2. Step 2: Correct approach is right = mid to keep mid in range

    To ensure peak is not skipped, right should be set to mid, not mid - 1.
  3. Final Answer:

    The else branch should set right = mid, not mid - 1 -> Option D
  4. Quick Check:

    Keep mid in range by right = mid [OK]
Hint: Use right = mid to avoid skipping mid element [OK]
Common Mistakes:
  • Setting right = mid - 1 causing missed peaks
  • Changing loop condition unnecessarily
  • Miscomputing mid index
5. You have an array nums = [10, 20, 15, 2, 23, 90, 67]. Using binary search to find a peak element, which index could be returned as a valid peak?
hard
A. 2
B. 5
C. 6
D. 3

Solution

  1. Step 1: Identify all peak elements in the array

    Peaks are elements greater than neighbors: 20 at index 1 and 90 at index 5.
  2. Step 2: Binary search can return any peak; index 5 (value 90) is a valid peak

    90 is greater than 23 and 67, so index 5 is a valid peak.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    90 at index 5 is peak [OK]
Hint: Binary search may return any peak, here index 5 is valid [OK]
Common Mistakes:
  • Assuming only one peak exists
  • Choosing non-peak elements
  • Ignoring neighbors when checking peak
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