Skip to content
Decoder

Longest Mountain in Array

Medium

Problem

A mountain is a contiguous stretch of a list with length at least 3 that strictly increases to a peak and then strictly decreases. The peak cannot be the first or last element of the stretch. Given a list arr, return the length of the longest mountain, or 0 if there is none.

Example 1

Input
arr = [2, 1, 4, 7, 3, 2, 5]
Output
5
Why
[1, 4, 7, 3, 2]

Example 2

Input
arr = [2, 2, 2]
Output
0
Why
flat is not a mountain

Example 3

Input
arr = [1, 2, 3]
Output
0
Why
no way down

Can you do it in one pass with O(1) extra space?

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.