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

Sorted list [3, 6, 9, 12, 15, 18, 21]. How many index triples i < j < k satisfy vals[i] + vals[j] = vals[k]?

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

Correct answer: A. 9

Fix the largest index k and use two pointers over the smaller values: a sum that equals vals[k] is a hit, a smaller sum moves the left pointer, and a larger sum moves the right pointer inward. Counting the at-most case gives a larger number that includes these triples.

More Coding Interview Problems questions

Exams that ask this topic