The Find in Mountain Array interview question is an optimization puzzle. A "mountain array" increases to a peak and then decreases. You are given a hidden mountain array (accessible only via an get(index) method) and a target value. You need to find the minimum index such that MountainArray.get(index) == target. You must minimize the number of calls to the get method.
This Find in Mountain Array coding problem is a frequent "Hard" challenge at Amazon and Google. It tests your ability to combine multiple Binary Search interview patterns. You can't just search linearly; you must use binary search to find the peak, then binary search the increasing side, and finally binary search the decreasing side. It evaluates your precision with search ranges and method-call budgets.
The problem is solved in three distinct Binary Search phases:
[0, peak] for the target.[peak + 1, length - 1].Mountain: [1, 2, 3, 4, 5, 3, 1], Target: 3.
[1, 2, 3, 4, 5]. Target 3 found at index 2.
Result: 2. (Even though 3 exists at index 5, 2 is smaller).get(index) too many times. You should store the result of get(mid) to avoid redundant calls.This is a "Triple Binary Search" problem. Ensure you can write a generalized binarySearch function that takes a flag for isAscending. This keeps your code modular and reduces errors during the high-pressure search phases.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Search in a Sorted Array of Unknown Size | Medium | Solve | |
| Find the Index of the Large Integer | Medium | Solve | |
| Number of Equal Numbers Blocks | Medium | Solve | |
| Leftmost Column with at Least a One | Medium | Solve | |
| Maximum Font to Fit a Sentence in a Screen | Medium | Solve |