Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

Sorted array a = [1, 2, 4, 5, 6, 9, 10, 11]. How many elements does the largest contiguous block of this array have if its largest element minus its smallest is at most 2?

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

Correct answer: B. 3

Two pointers keep the window whose spread is at most 2 and track the largest window size; because the array is sorted, the spread equals the last element minus the first. Block [4, 5, 6] is one of the windows of the maximum size.

More Coding Interview Problems questions

Exams that ask this topic