Skip to content
Decoder

Longest Turbulent Subarray

Medium

Problem

A subarray is turbulent if the comparison sign flips between each adjacent pair: up, down, up, ... or down, up, down, ... (for example a < b > c < d). Equal neighbours break it. Given arr, return the length of the longest turbulent subarray. A single element counts as turbulent.

Example 1

Input
arr = [9, 4, 2, 10, 7, 8, 8, 1, 9]
Output
5
Why
[4, 2, 10, 7, 8]

Example 2

Input
arr = [4, 8, 12, 16]
Output
2

Example 3

Input
arr = [100]
Output
1

Constraints

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

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.