Skip to the page

DSA · Build it

3Sum variant (LeetCode 15)

The interviewer asks

Find all unique triplets in the array that sum to a target.

Then

Follow-ups the interviewer asks next

  1. The closest sum to a target.
  2. Count triplets with sum below target.
  3. 4Sum.

The answer

What a strong answer covers

  • sort; fix i; two pointers lo/hi; skip duplicates at i, lo and hi; O(n²) time, O(1) extra beyond the sort; an explanation of why hashing doesn't beat O(n²) here.

The red pen

Common traps

Avoid:duplicate triplets; a Set of stringified triplets as the only dedupe; O(n³).

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview