Find the First Array Element Smaller Than Both of Its Neighbors
Company: Capital One
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: A coding problem that asks for the first element of an integer array whose left and right neighbors are both strictly larger than it, returning -1 when no element qualifies. It tests careful boundary handling, strict comparisons when neighbors are equal, and precise output rules.
Read the full Capital One Software Engineer interview experience this question came from
Constraints
- 1 <= len(nums) <= 10^5
- 0 <= nums[i] <= 10^9, so the result -1 can never be confused with an element
- Every value and the result fit in a 32-bit signed integer (none exceeds 2^31 - 1)
Examples
Input: ([5, 3, 4, 1, 2],)
Expected Output: 3
Explanation: Source example 1: indices 1 (value 3) and 3 (value 1) both qualify; the earliest index wins, not the smallest value.
Input: ([2, 2, 7, 6, 9, 0],)
Expected Output: 6
Explanation: Source example 2: index 1 has an equal left neighbor, index 2 is a peak, and index 3 (value 6) has neighbors 7 and 9.
Hints
- Only indices strictly between the first and last positions have two neighbors, so the endpoints can never be the answer.
- Both comparisons are strict: a neighbor equal to the element does not count as larger.
- When several elements qualify, the answer is decided by position, not by value.