Given an array of integers, find all unique triplets that sum to zero. The solution set must not contain duplicate triplets.
For example, given nums = [-1, 0, 1, 2, -1, -4], the output is [[-1, -1, 2], [-1, 0, 1]].
Explain your approach, how you avoid duplicate triplets, and analyze the time and space complexity.
How to approach it
- Hint 1
Sorting the array first makes it easier to skip duplicates and apply two pointers.
- Hint 2
Fix one element, then use two pointers on the remaining sorted portion to find pairs that sum to the negative of the fixed element.
- Hint 3
To avoid duplicates, skip over consecutive identical values when advancing the fixed element or the two pointers.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.