Data Structures & AlgorithmsModerate

For every window of exactly 3 consecutive elements of 7 3 9 4 8 2 6, compute (maximum − minimum) inside that window. What is the SMALLEST such value?

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

Correct answer: B. 5

A deque-based sliding window gives the maximum and minimum of each length-3 window; the answer is the smallest spread among them. Using length-2 windows, the largest spread, or only the outer elements tests different things.

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

Exams that ask this topic