📖 Scenario: Imagine you have a list of numbers representing heights of hills along a trail. You want to find a hill that is taller than its neighbors, called a peak.
🎯 Goal: Build a program that finds a peak hill using a fast method called binary search.
📋 What You'll Learn
Create a list called hills with exact heights
Create two variables left and right to mark search range
Use a while loop with binary search logic to find a peak
Print the index of a peak element found
💡 Why This Matters
🌍 Real World
Finding peaks in data can help identify local maximums in sensor readings, stock prices, or terrain elevations.
💼 Career
Binary search is a fundamental algorithm used in software engineering for efficient searching and optimization problems.
Progress0 / 4 steps
1
Create the hills list
Create a list called hills with these exact heights: [1, 3, 20, 4, 1, 0]
DSA Python
Hint
Use square brackets and commas to create the list.
2
Set search boundaries
Create two variables called left and right. Set left to 0 and right to the last index of hills using len(hills) - 1.
DSA Python
Hint
Remember list indexes start at 0, and last index is length minus one.
3
Find the peak using binary search
Use a while loop with condition left < right. Inside the loop, create mid as the middle index using (left + right) // 2. If hills[mid] is less than hills[mid + 1], set left to mid + 1. Otherwise, set right to mid.
DSA Python
Hint
Compare middle element with its next neighbor to decide which side to search.
4
Print the peak index
Print the value of left, which is the index of a peak element.
DSA Python
Hint
The peak index is where the search ends, stored in left.
Practice
(1/5)
1. What is a peak element in an array when using binary search to find it?
easy
A. The first element of the array
B. An element that is greater than its neighbors
C. The smallest element in 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: Relate to binary search goal
Binary search finds such an element by comparing middle elements with neighbors to decide search direction.
Final Answer:
An element that is greater than its neighbors -> Option B
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 Python syntax to start a binary search loop for finding a peak element in array nums?
easy
A. while left <= right:
B. for i in nums:
C. if left < right:
D. while left < right:
Solution
Step 1: Identify binary search loop condition
Binary search typically uses while left < right: to narrow down the search space until left meets right.
Step 2: Compare options
while left <= right: is common in classic binary search but for peak finding, while left < right: is preferred to avoid infinite loops.
Final Answer:
while left < right: -> Option D
Quick Check:
Binary search loop uses < not <= [OK]
Hint: Use while left < right for peak element binary search [OK]
Common Mistakes:
Using for loop instead of while
Using <= which may cause infinite loop
Using if instead of loop
3. What will be the output index of the peak element for the array nums = [1, 3, 20, 4, 1, 0] using binary search peak finding?
medium
A. 2
B. 1
C. 3
D. 0
Solution
Step 1: Identify peak candidates
Check elements: 20 at index 2 is greater than neighbors 3 and 4, so index 2 is a peak.
Step 2: Binary search logic
Binary search will find index 2 by comparing mid elements and moving left or right accordingly.
Final Answer:
2 -> Option A
Quick Check:
Peak at index 2 with value 20 [OK]
Hint: Peak is local max, here 20 at index 2 [OK]
Common Mistakes:
Choosing first or last element incorrectly
Confusing index with value
Not checking both neighbors
4. Given this code snippet to find a peak element, what is the error?
def find_peak(nums):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] < nums[mid + 1]:
left = mid + 1
else:
right = mid - 1
return left
medium
A. Index mid + 1 may cause out of range error
B. Return should be right instead of left
C. Loop condition should be while left < right
D. Mid calculation is incorrect
Solution
Step 1: Check index usage in condition
The code accesses nums[mid + 1] without checking if mid + 1 is within array bounds.
Step 2: Identify potential error
If mid is at the last index, mid + 1 causes an out of range error.
Final Answer:
Index mid + 1 may cause out of range error -> Option A
Quick Check:
Accessing nums[mid + 1] risks out of range [OK]
Hint: Check array bounds before accessing mid+1 [OK]
Common Mistakes:
Ignoring array boundary checks
Using <= in loop causing infinite loop
Returning wrong index
5. You have an array nums = [10, 20, 15, 2, 23, 90, 67]. Using binary search, which index will be returned as a peak element? Consider multiple peaks exist.
hard
A. 1
B. 4
C. 5
D. 6
Solution
Step 1: Identify all peak elements
Peaks are at index 1 (20 > 10 and 15), index 5 (90 > 23 and 67).
Step 2: Understand binary search behavior
Binary search may return any peak, but typically it finds the one in the middle or right side depending on comparisons.
Step 3: Trace binary search on given array
Mid starts near center; since nums[mid] < nums[mid+1] at some point, search moves right, finally returning index 5.
Final Answer:
5 -> Option C
Quick Check:
Binary search finds peak at index 5 with value 90 [OK]
Hint: Binary search may return any peak; here index 5 is found [OK]
Common Mistakes:
Assuming only first peak is returned
Ignoring binary search direction logic
Confusing index with value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.