Skip to main content

15. 3Sum

mediumAsked at HubSpot

HubSpot asks 3Sum to test your ability to reduce a multi-pointer problem systematically — a key skill when de-duplicating and reconciling overlapping data records across their CRM's contact merge workflows.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in HubSpot loops.

  • Glassdoor (2026-Q1)HubSpot SWE onsite reports list 3Sum as a common medium-difficulty problem in coding rounds.
  • Blind (2025-11)Multiple HubSpot threads cite 3Sum as a frequent test of two-pointer and deduplication skills.

Problem

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.

Constraints

  • 3 <= nums.length <= 3000
  • -10^5 <= nums[i] <= 10^5

Examples

Example 1

Input
nums = [-1,0,1,2,-1,-4]
Output
[[-1,-1,2],[-1,0,1]]

Explanation: Sort the array, fix each element, then use two pointers on the rest.

Example 2

Input
nums = [0,0,0]
Output
[[0,0,0]]

Explanation: Only one unique triplet even though all three indices differ.

Approaches

1. Sort + two pointers

Sort the array. Fix the first element i and run two pointers (left = i+1, right = end) to find pairs summing to -nums[i]. Skip duplicates at each level to avoid repeated triplets.

Time
O(n²)
Space
O(1) excluding output
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (nums[i] > 0) break; // sorted — no triplet possible
    if (i > 0 && nums[i] === nums[i - 1]) continue; // skip duplicate i
    let left = i + 1, right = nums.length - 1;
    while (left < right) {
      const sum = nums[i] + nums[left] + nums[right];
      if (sum === 0) {
        result.push([nums[i], nums[left], nums[right]]);
        while (left < right && nums[left] === nums[left + 1]) left++;
        while (left < right && nums[right] === nums[right - 1]) right--;
        left++; right--;
      } else if (sum < 0) {
        left++;
      } else {
        right--;
      }
    }
  }
  return result;
}

Tradeoff: O(n²) time — optimal for this problem. Sorting enables the two-pointer sweep and makes deduplication trivial via adjacent comparison. The three skip conditions (i duplicate, left duplicate, right duplicate) are the most error-prone part — walk through them explicitly.

HubSpot-specific tips

Start with the reduction: '3Sum is Two Sum with a fixed outer element — sort first, then run two pointers.' HubSpot interviewers specifically watch for the deduplication logic; they will hand you a test case like [-2,0,0,2,2] and ask why your code doesn't return duplicates. Explain each skip condition verbally before coding it. Breaking early when nums[i] > 0 shows optimization awareness.

Common mistakes

  • Forgetting to skip duplicate values for the outer i loop — produces duplicate triplets.
  • Skipping duplicates before recording the found triplet instead of after — misses valid answers.
  • Not breaking early when nums[i] > 0 — a sorted array with a positive first element can never sum to 0.
  • Using a Set of arrays for deduplication — arrays are compared by reference in JS, so this doesn't work; rely on sorting and index skipping instead.

Follow-up questions

An interviewer at HubSpot may pivot to one of these next:

  • 3Sum Closest (LC 16) — find the triplet sum closest to a target.
  • 4Sum (LC 18) — add one more fixed pointer; same pattern generalized.
  • How would you solve k-Sum generically using recursion?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why sort first?

Sorting lets the two-pointer technique work (moving left/right based on sum direction) and makes deduplication trivial — same values are adjacent, so a simple equality check skips them.

Can this be done in O(n) time?

No — 3Sum has a proven lower bound of Ω(n²) under the algebraic decision tree model for integer inputs without additional structure.

Why use two pointers instead of a hash set for the inner pair?

A hash set approach is also O(n²) but requires extra space and careful deduplication. Two pointers are O(1) extra space and more natural after sorting.

Companies that also ask 3Sum