Katkat

Find Any Local Minimum with Iterative Binary Search

MetaSoftware Engineer · Onsite

Last updated

Find the index of any strict local minimum in a nonempty array whose adjacent values differ, using an iterative logarithmic-time algorithm. The task tests search invariants, boundary-as-infinity semantics, singleton and monotone arrays, and correctness when several indices are valid.

Find Any Local Minimum with Iterative Binary Search

Given a nonempty integer array numbers, return the index of any local minimum. An index i is a local minimum when its value is strictly smaller than each neighbor that exists. Treat the missing neighbor beyond either boundary as positive infinity.

Adjacent elements are guaranteed to be different. Your implementation must be iterative and run in O(log n) time.

Function Signature

def find_local_minimum(numbers: list[int]) -> int:
 ...

Constraints

  • 1 <= len(numbers) <= 1_000_000
  • numbers[i] != numbers[i + 1] for every valid adjacent pair.
  • -1_000_000_000 <= numbers[i] <= 1_000_000_000.
  • If multiple local minima exist, return any valid index.

Examples

Input: numbers = [9, 7, 3, 5, 8]
Output: 2
Input: numbers = [1, 4, 3]
Output: 0 or 2
Input: numbers = [6]
Output: 0