โ† Core Algorithms
#9 most askedVery commonMediumTwo pointers

3Sum

New to Two pointers? Learn it from scratch first
Two Pointers & Stacks โ†’
๐Ÿข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

Return all unique triplets [a, b, c] in nums with a + b + c == 0. Each triplet should be sorted, and the list of triplets sorted too.

[-1, 0, 1, 2, -1, -4] โ†’ [[-1, -1, 2], [-1, 0, 1]]

๐Ÿงช Practice

Write your solution, then press Run tests. Explain your approach out loud as you go.

Python
def three_sum(nums):
    pass

Output appears here. Press Run or Ctrl/โŒ˜ + Enter.

Try it first. Struggling for a few minutes is how it sticks.