Challenge: Three Sum

A very common interview question. Implement ThreeSum then click Validate — your code is compiled and tested live.

The problem
  • Given an integer array nums, return all unique triplets [a, b, c] such that a + b + c == 0.
  • The solution set must not contain duplicate triplets.
  • The order of the triplets and of the numbers inside each triplet does not matter.
  • Example: nums = [-1, 0, 1, 2, -1, -4] → [[-1, -1, 2], [-1, 0, 1]].
Hints (click to show)
  1. Sort the array first — it makes duplicates easy to skip.
  2. Fix one number, then use the two-pointer technique on the rest of the array.
  3. Skip equal neighboring values to avoid producing duplicate triplets. Overall O(n²).