Find in Mountain Array - Problem
Interactive Mountain Array Search Challenge

Imagine you're exploring a mountain range represented as an array where the elevation strictly increases to a peak, then strictly decreases. This special structure is called a mountain array.

A mountain array arr satisfies:
arr.length >= 3
• There exists a peak index i where 0 < i < arr.length - 1
• Values strictly increase: arr[0] < arr[1] < ... < arr[i-1] < arr[i]
• Values strictly decrease: arr[i] > arr[i+1] > ... > arr[arr.length-1]

Your Mission: Find the minimum index where mountainArr.get(index) == target. Return -1 if not found.

The Catch: You can't access the array directly! You can only use:
MountainArray.get(k) - returns element at index k
MountainArray.length() - returns array length
Maximum 100 API calls allowed - exceed this and get Wrong Answer!

This constraint forces you to think strategically about minimizing array accesses.

Input & Output

example_1.py — Basic Mountain Search
$ Input: mountain_arr = [1,2,3,4,5,3,1], target = 3
Output: 2
💡 Note: Target 3 appears at indices 2 and 5. We return 2 because it's the minimum index. The algorithm finds peak at index 4 (value 5), searches ascending side [1,2,3,4,5] and finds 3 at index 2.
example_2.py — Target Not Found
$ Input: mountain_arr = [0,1,2,4,2,1], target = 3
Output: -1
💡 Note: Target 3 doesn't exist in the mountain array. Peak is at index 3 (value 4). Neither ascending side [0,1,2,4] nor descending side [4,2,1] contains 3.
example_3.py — Target Only on Descending Side
$ Input: mountain_arr = [1,5,2], target = 2
Output: 2
💡 Note: Peak is at index 1 (value 5). Target 2 is not found in ascending side [1,5], but exists at index 2 in descending side [5,2].

Visualization

Tap to expand
Mountain Array Search Strategy1234PEAK531Ascending Binary SearchDescending Binary Search🔍 Search ascending side first for minimum index, then descending if needed
Understanding the Visualization
1
Find the Peak
Use binary search to locate the highest treasure (peak of the mountain)
2
Search Ascending Side
Search the left side where treasures increase in value using ascending binary search
3
Search Descending Side
If not found, search the right side where treasures decrease using descending binary search
Key Takeaway
🎯 Key Insight: By finding the peak first, we can split the mountain into two sorted halves and search each efficiently, using only ~3 log n API calls total.

Time & Space Complexity

Time Complexity
⏱️
O(log n)

Three binary searches, each taking O(log n) time

n
2n
Linearithmic
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
Linear Space

Constraints

  • 3 <= mountain_arr.length() <= 104
  • 0 <= target <= 109
  • 0 <= mountain_arr.get(index) <= 109
  • mountain_arr is guaranteed to be a mountain array
  • Maximum 100 calls to MountainArray.get() allowed
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
52.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen