Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.peakIndexInMountainArray(arr). arr strictly rises to one interior peak and then strictly falls. Return the peak index in O(log n).

Starter code

class Solution:
    def peakIndexInMountainArray(self, arr):
        pass
Test cases

short-mountain

{
  "args": [
    [
      0,
      2,
      1,
      0
    ]
  ]
}

Expected: 1

Wizard outline
  1. Step 1: Initialize Solution.peakIndexInMountainArray

    Replace the empty starter with the first real state owned by Solution.peakIndexInMountainArray. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Pass the Center Peak case

    Complete the readable core algorithm for one representative Interview case. Use the local slope to retain the side containing the unique mountain peak.

  3. Step 3: Harden the Short Mountain boundary

    Repair the reviewed boundary and pass the complete submission contract. Before the unique peak every adjacent slope rises, and from the peak onward every adjacent slope falls. Each comparison therefore discards only indices that cannot be the turning point, preserving the peak until the interval converges.

Footguns and prerequisites
  • Return the peak index rather than its value.
  • arrays strings two pointers sliding window
Reviewed references
Practice prerequisites
  • Find the First True Boundary(opens in a new tab)

    Find the First True Boundary isolates every index before left is known false, while every index at or after right is known true or the sentinel len(flags). That focused state discipline is required when implementing peak index mountain array as a complete Interview Problem.

Recommended approach and implementation

Binary-search the slope between middle and middle+1. An ascending slope puts the peak strictly right; a descending slope keeps middle and the left side.

Why it works: Before the unique peak every adjacent slope rises, and from the peak onward every adjacent slope falls. Each comparison therefore discards only indices that cannot be the turning point, preserving the peak until the interval converges.

class Solution:
    def peakIndexInMountainArray(self, arr):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        left, right = 0, len(arr) - 1
        while left < right:
            middle = (left + right) // 2
            if arr[middle] < arr[middle + 1]:
                left = middle + 1
            else:
                right = middle
        return left
Similar exercises