Valid Mountain Array

EasyArrays & Hashingtwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

An array is a **mountain** if it has at least 3 elements and there is some index p with 0 < p < arr.length - 1 such that the values strictly increase from index 0 up to p, then strictly decrease from p to the last index. Given an integer array arr, return true if it is a mountain and false otherwise. Plateaus (two equal neighbours) anywhere break the mountain, and an array that only rises or only falls is not a mountain. Aim for a single O(n) pass.

Examples

Input: arr = [0,3,2,1]
Output: true
It rises to 3 at index 1, then falls strictly to the end.
Input: arr = [3,5,5]
Output: false
The two 5s form a plateau, so the sequence is not strictly monotone on either side.
Input: arr = [2,1]
Output: false
Fewer than 3 elements can never form a mountain.

Constraints

  • 1 <= arr.length <= 10^4
  • 0 <= arr[i] <= 10^4
  • Return a boolean.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems