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

Sorted list [1, 2, 4, 6, 7, 9, 11]. How many index pairs i < j satisfy vals[i] + vals[j] <= 10?

  1. A. 12
  2. B. 9
  3. C. 7
  4. D. 18

Correct answer: B. 9

Two pointers from both ends: when vals[l] + vals[r] ≤ 10, all r − l pairs with left end l qualify, giving 9 in total. Using < 10 misses the boundary pairs 1 + 9 and 4 + 6 (7), letting j start at i adds 1+1, 2+2, 4+4 (12), and counting ordered pairs gives 18.

More Coding Interview Problems questions

Exams that ask this topic