Longest Mountain in Array

MediumTwo Pointerstwo 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

A *mountain* is a contiguous subarray of length at least 3 that strictly increases up to a peak and then strictly decreases, with the peak strictly inside (not the first or last element of the subarray). Plateaus (equal neighbors) break a mountain. Given an integer array arr, return the length of the longest mountain subarray, or 0 if there is none. Aim for one pass and O(1) extra space.

Examples

Input: arr = [1,3,5,4,2,6,6,1]
Output: 5
The subarray [1,3,5,4,2] climbs to 5 and descends to 2.
Input: arr = [3,3,3]
Output: 0
No strict climb exists.

Constraints

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

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