Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

Array [5, 1, 4, 6, 2, 8, 3, 7]. What is the length of the longest contiguous subarray whose maximum minus minimum is at most 3?

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

Correct answer: A. 2

Use a sliding window with a max-deque and a min-deque: extend the right end, and while max minus min exceeds 3, move the left end. Keeping only a running max or min without the deques cannot track values that leave the window.

More Coding Interview Problems questions

Exams that ask this topic