Summit Seeker: Find a Peak
๐ฆ Summit Seeker: Find a Peak
Difficulty: Intermediate Tags: binary-search, arrays, divide-and-conquer Series: CS 102: Problem Solving & Logic
Problem
The Summit Seeker finds the highest peaks in magical mountains. Find any peak element in an array where a peak is strictly greater than its neighbors.
A peak element is an element that is strictly greater than its neighbors. Given an array of integers, return the index of any peak element.
Peak Definition:
- For middle elements: arr[i] > arr[i-1] AND arr[i] > arr[i+1]
- For first element: arr[0] > arr[1] (only right neighbor)
- For last element: arr[n-1] > arr[n-2] (only left neighbor)
- Out-of-bounds neighbors treated as -โ
You may return the index of any peak if multiple exist.
Real-World Application
Peak finding is used in: - Signal processing - detecting local maxima in waveforms - Image analysis - finding bright spots, corners, features - Time series analysis - identifying price peaks, trend changes - Terrain mapping - finding mountain summits in elevation data - Network traffic - detecting usage spikes - Audio processing - finding amplitude peaks for beat detection - Machine learning - local optima in optimization landscapes - Data compression - identifying significant data points
Input
data = List[int] # Array of integers
Output
int # Index of any peak element
Constraints
- 1 โค length โค 100,000
- Array values are integers
- At least one peak is guaranteed to exist
- Array may contain duplicates
- Can return index of any peak
Examples
Example 1: Single Peak
Input: [1, 2, 3, 1]
Output: 2
Explanation:
3 โ Peak at index 2
/ \
2 1
/
1
Element at index 2 (value 3) is greater than neighbors (2 and 1).
Example 2: Multiple Peaks
Input: [1, 2, 1, 3, 5, 6, 4]
Output: 1 or 5 (both valid)
Explanation:
6 โ Peak at index 5
/ \
2 5 4
/ \ /
1 1 3
- Index 1 (value 2): 2 > 1 and 2 > 1 โ
- Index 5 (value 6): 6 > 5 and 6 > 4 โ
Both are valid answers.
Example 3: Ascending Array
Input: [1, 2, 3, 4, 5]
Output: 4
Explanation: Last element (5) is peak because it's greater than 4, and right neighbor is -โ.
Example 4: Descending Array
Input: [5, 4, 3, 2, 1]
Output: 0
Explanation: First element (5) is peak because it's greater than 4, and left neighbor is -โ.
Example 5: Single Element
Input: [42]
Output: 0
Explanation: Single element is always a peak (both neighbors are -โ).
Example 6: All Same Values
Input: [5, 5, 5, 5]
Output: -1 or any index (implementation dependent)
Explanation: No element is strictly greater than neighbors.
Example 7: Valley Pattern
Input: [3, 1, 2]
Output: 0 or 2
Explanation:
3 2
\ /
\ /
\ /
1
- Index 0: 3 > 1 and left is
-โโ - Index 2: 2 > 1 and right is
-โโ
What You'll Learn
- Binary search on non-sorted arrays
- Divide and conquer with guarantees
- Comparing elements to find direction
- Working with array boundaries
- Multiple valid solutions handling
Why This Matters
Peak finding is a classic binary search variation that teaches: - Binary search doesn't require sorted arrays - Using comparisons to guarantee solution in one direction - Working with invariants and proofs - Real-world applications beyond searching
Companies that ask this: Google, Facebook, Amazon, Microsoft, LinkedIn
Starter Code
def challenge_function(data):
"""
Find the index of any peak element.
A peak element is an element that is strictly greater than its neighbors.
You may assume that out-of-bounds neighbors are -infinity.
Args:
data (List[int]): array of integers
Returns:
int: index of any peak element
"""
# Your implementation here
pass