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

Sorted array a = [1, 2, 4, 7, 11, 16, 22, 29]. How many index pairs i < j have a[i] + a[j] <= 20?

  1. A. 13
  2. B. 1
  3. C. 15
  4. D. 12

Correct answer: A. 13

For each i, the valid j form a block starting at i+1, and as i increases the block shrinks, so a pointer that moves left works in linear time. Pairs with sum exactly 20 are included, which is why the strict bound differs.

More Coding Interview Problems questions

Exams that ask this topic