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

Sorted array a = [-2, -1, -1, 0, 1, 1, 2, 2]. How many DISTINCT value triples (x, y, z) with x <= y <= z taken from the array's values sum to 0? Two index triples with the same values count once.

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

Correct answer: C. 4

Equal values must be skipped at each pointer so that each value triple is produced once. The distinct zero-sum value triples are [(-2, 0, 2), (-2, 1, 1), (-1, -1, 2), (-1, 0, 1)], which is 4 in total; counting index triples gives 9.

More Coding Interview Problems questions

Exams that ask this topic