Data Structures & AlgorithmsModerateTCS NQT · Product Company SDE

Problem: In the sorted array [−3, −1, 0, 1, 2, 3], how many pairs of distinct positions (i < j) have a sum of exactly 0?

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

Correct answer: B. 2

Two pointers from both ends find the pairs (−3, 3) and (−1, 1); the zero element pairs with nothing else, so there are two pairs.

More Coding Interview Problems questions

Exams that ask this topic