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

Sorted array a = [-3, -1, 0, 2, 4, 5, 9]. How many index pairs i < j have -1 <= a[i] + a[j] <= 5?

  1. A. 10
  2. B. 8
  3. C. 7
  4. D. 12

Correct answer: A. 10

Count pairs with sum ≤ 5 (12) and subtract those with sum ≤ −2 (2), each with a linear two-pointer pass: 12 − 2 = 10. A strict lower bound loses the two pairs summing to exactly −1 (8), making both bounds strict also loses 0 + 5 (7), and ignoring the lower bound gives 12.

More Coding Interview Problems questions

Exams that ask this topic