EvalFrame Technical Mock Interviews & Coding Practice

3Sum

Medium · Array / Two Pointers · Algorithm problem · all problems

Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`. Notice that the solution set must not contain duplicate triplets. **Examples:** ```typescript threeSum([-1, 0, 1, 2, -1, -4]) // [[-1, -1, 2], [-1, 0, 1]] threeSum([0, 1, 1]) // [] threeSum([0, 0, 0]) // [[0, 0, 0]] ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(n^2)
  • Space complexity: O(1)

Sorting takes O(n log n), followed by n two-pointer scans taking O(n) each, yielding O(n^2).

Follow-Up Questions Interviewers Ask About 3Sum

  • How do you avoid duplicate triplets in the output?
    Skip identical adjacent elements for both the outer loop i and inner two-pointers l and r after finding a valid triplet.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.