Data Structures & AlgorithmsHardProduct-company MCQ pattern

How many UNIQUE triplets (as sets of values, ignoring order) of [−1, 0, 1, 2, −1, −4, −2, 3] sum to 0? (Sort + two pointers, skipping duplicates.)

  1. A. 6
  2. B. 5
  3. C. 4
  4. D. 7

Correct answer: B. 5

The unique triplets are (−4, 1, 3), (−2, −1, 3), (−2, 0, 2), (−1, −1, 2) and (−1, 0, 1), so 5. Without skipping the repeated −1, triplets such as (−1, 0, 1) and (−2, −1, 3) are found once for each −1 and the count comes out as 7.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic