Find First Local Minimum
Company: Uber
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates array-processing and algorithmic reasoning skills, specifically the ability to identify local minima and handle index-based and boundary-case conditions.
Constraints
- 0 <= len(nums) <= 100000
- -1000000000 <= nums[i] <= 1000000000
Examples
Input: [5, 3, 4, 2, 6]
Expected Output: 1
Explanation: Index 1 is a local minimum because 3 < 5 and 3 < 4. Although index 3 is also a local minimum, the first one is at index 1.
Input: [2, 3, 4]
Expected Output: 0
Explanation: Index 0 is a local minimum because 2 < 3.
Input: [4, 3, 2]
Expected Output: 2
Explanation: Index 2 is a local minimum because 2 < 3. No earlier index satisfies the condition.
Input: [7]
Expected Output: 0
Explanation: A single-element array always has index 0 as a local minimum.
Input: []
Expected Output: -1
Explanation: An empty array has no local minimum.
Input: [1, 1, 1]
Expected Output: -1
Explanation: All comparisons are strict, and no element is strictly smaller than its neighbor(s).
Input: [2, 1]
Expected Output: 1
Explanation: Index 1 is a local minimum because 1 < 2.
Input: [3, 2, 2, 1]
Expected Output: 3
Explanation: Indices 1 and 2 are not local minima because strict inequality fails due to equal values. Index 3 is a local minimum because 1 < 2.
Hints
- You only need to compare each element with its immediate neighbors, not the whole array.
- Handle the boundary indices `0` and `n - 1` separately, then scan from left to right to find the first valid position.