By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(A Complete FAANG-Level Interview Guide)
"This problem appears in 1 out of every 3 onsite interviews—master it, and you’ll prove you can optimize brute-force solutions into logarithmic time while handling edge cases like a pro."
Before tackling peak elements, ensure you understand: 1. Binary Search – How to modify it for non-sorted arrays. 2. Array Traversal – Linear vs. logarithmic time complexity. 3. Edge Cases – Handling single-element arrays, plateaus, and boundaries.
nums[mid] < nums[mid+1]
nums[mid] == nums[mid+1]
nums[0] > nums[1]
0
mid
(Repeatable mental checklist for every peak element problem.)
Ask: "Are duplicates allowed?"
Choose the Right Approach
If O(log n) is required → Modified Binary Search (interviewers expect this).
Implement Binary Search (Modified)
left = 0
right = len(nums) - 1
left < right
mid = left + (right - left) // 2
nums[mid] < nums[mid + 1]
left = mid + 1
right = mid
Return left (or right, since they converge).
left
right
Handle Edge Cases
Plateaus? → Move right until a higher neighbor is found.
Verify with Examples
[1, 2, 3, 1]
2
[1, 2, 1, 2, 1]
1
3
Test with [1] → Peak at 0.
[1]
Analyze Time & Space Complexity
Problem: Find a peak element in an array where nums[i] ≠ nums[i+1] for all i. Input: [1, 2, 3, 1] Output: 2 (index of 3)
nums[i] ≠ nums[i+1]
i
python def findPeakElement(nums): left, right = 0, len(nums) - 1 while left < right: mid = left + (right - left) // 2 if nums[mid] < nums[mid + 1]: left = mid + 1 else: right = mid return left
Time Complexity: O(log n) Space Complexity: O(1)
Problem: Find any peak element (array may have duplicates). Input: [1, 2, 2, 3, 1] Output: 3 (index of 3)
[1, 2, 2, 3, 1]
python def findPeakElement(nums): left, right = 0, len(nums) - 1 while left < right: mid = left + (right - left) // 2 if nums[mid] < nums[mid + 1]: left = mid + 1 elif nums[mid] > nums[mid + 1]: right = mid else: # nums[mid] == nums[mid + 1] (plateau) # Move right until a higher neighbor is found temp = mid + 1 while temp < right and nums[temp] == nums[mid]: temp += 1 if nums[temp] > nums[mid]: left = temp else: right = mid return left
Time Complexity: O(log n) (worst case O(n) if all elements are equal). Space Complexity: O(1)
Problem: Find a peak in a 2D matrix (a cell greater than its 4 neighbors). Input:
[ [10, 20, 15], [21, 30, 14], [ 7, 16, 32] ]
Output: 30 (or 32)
30
32
python def findPeakGrid(mat): rows, cols = len(mat), len(mat[0]) left, right = 0, cols - 1 while left <= right: mid_col = left + (right - left) // 2 max_row = 0 # Find the max element in the current column for i in range(rows): if mat[i][mid_col] > mat[max_row][mid_col]: max_row = i # Check left and right neighbors left_neighbor = mat[max_row][mid_col - 1] if mid_col > 0 else -1 right_neighbor = mat[max_row][mid_col + 1] if mid_col < cols - 1 else -1 if mat[max_row][mid_col] > left_neighbor and mat[max_row][mid_col] > right_neighbor: return [max_row, mid_col] elif left_neighbor > mat[max_row][mid_col]: right = mid_col - 1 else: left = mid_col + 1 return [-1, -1]
Time Complexity: O(rows log cols) Space Complexity: O(1)
mid+1
nums[0]
nums[-1]
left = mid
mid - 1
nums[-1] > nums[-2]
len(nums)-1
"Alright, let’s lock this in. Here’s your 30-second peak-finding checklist before walking into that interview:
[1, 2, 2, 1]
Remember: Binary search isn’t just for sorted arrays. It works here because the peak creates a monotonic property—use it to your advantage. Now go crush that interview!
You’ve got this. Now go solve it like a FAANG engineer. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.