Check Whether an Array Strictly Rises to One Peak and Then Strictly Falls

Quick Overview

Decide whether an integer array rises strictly to a single interior peak and then falls strictly to its last element. It tests a linear scan that handles equal neighbors, arrays shorter than three, and a peak at the first or last index.

Check Whether an Array Strictly Rises to One Peak and Then Strictly Falls

Company: Flipkart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `arr`, return `True` if and only if it is a valid mountain array. `arr` is a mountain array if and only if `len(arr) >= 3` and there is some index `i` with `0 < i < len(arr) - 1` such that: - `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]` - `arr[i] > arr[i + 1] > ... > arr[len(arr) - 1]` Both comparisons are strict, so two equal neighboring values anywhere in the array mean it is not a mountain. ### Function Signature ```python def valid_mountain_array(arr: list[int]) -> bool: ``` ### Constraints - `1 <= len(arr) <= 10^4` - `0 <= arr[i] <= 10^4` ### Examples ```text Input: arr = [2,1] Output: False ``` The array has fewer than 3 elements, so it cannot be a mountain. ```text Input: arr = [3,5,5] Output: False ``` The values rise from 3 to 5, but the two 5s are equal, so the part after the peak is not strictly decreasing. ```text Input: arr = [0,3,2,1] Output: True ``` The values rise strictly from 0 to 3 at index 1, which is neither the first nor the last index, and then fall strictly from 3 to 1.

Overview: Decide whether an integer array rises strictly to a single interior peak and then falls strictly to its last element. It tests a linear scan that handles equal neighbors, arrays shorter than three, and a peak at the first or last index.

Flipkart logo
Flipkart
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers arr, return True if and only if it is a valid mountain array.

arr is a mountain array if and only if len(arr) >= 3 and there is some index i with 0 < i < len(arr) - 1 such that:

  • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
  • arr[i] > arr[i + 1] > ... > arr[len(arr) - 1]

Both comparisons are strict, so two equal neighboring values anywhere in the array mean it is not a mountain.

Function Signature

def valid_mountain_array(arr: list[int]) -> bool:

Constraints

  • 1 <= len(arr) <= 10^4
  • 0 <= arr[i] <= 10^4

Examples

Input: arr = [2,1]
Output: False

The array has fewer than 3 elements, so it cannot be a mountain.

Input: arr = [3,5,5]
Output: False

The values rise from 3 to 5, but the two 5s are equal, so the part after the peak is not strictly decreasing.

Input: arr = [0,3,2,1]
Output: True

The values rise strictly from 0 to 3 at index 1, which is neither the first nor the last index, and then fall strictly from 3 to 1.