Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft

A mountain is a contiguous subarray of length ≥ 3 that strictly increases to a single peak and then strictly decreases. What is the length of the longest mountain in [5, 4, 5, 3, 1, 5, 2, 3, 2, 1, 4, 6] (0 if none)?

  1. A. 3
  2. B. 5
  3. C. 4
  4. D. 6

Correct answer: C. 4

For each peak, extend left while values strictly rise towards it and right while they strictly fall; plateaus break a mountain. The longest has length 4.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic