Skip to main content

35. 3Sum

mediumAsked at Plaid

Find all unique triplets that sum to zero. Plaid asks this because three-way reconciliation across a debit, a credit, and a fee is exactly this primitive — find three rows that net to zero.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid SWE II onsite — framed as 3-way reconciliation.
  • Blind (2026)Plaid backend OA.

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. 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]]

Example 2

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

Approaches

1. Triple nested loop with Set dedup

Try every (i, j, k); dedupe by sorting each triplet.

Time
O(n^3)
Space
O(n^3) for the dedup set
// Cubic, with extra dedup overhead. Mention only as the starting point.

Tradeoff: O(n^3) — TLE on n=3000.

2. Sort + fixed pointer + two-pointer scan

Sort. For each i, use two pointers (lo, hi) on the rest of the array to find pairs summing to -nums[i]. Skip duplicates at each level.

Time
O(n^2)
Space
O(1) extra
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const out = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (i > 0 && nums[i] === nums[i - 1]) continue;
    let lo = i + 1, hi = nums.length - 1;
    while (lo < hi) {
      const s = nums[i] + nums[lo] + nums[hi];
      if (s === 0) {
        out.push([nums[i], nums[lo], nums[hi]]);
        while (lo < hi && nums[lo] === nums[lo + 1]) lo++;
        while (lo < hi && nums[hi] === nums[hi - 1]) hi--;
        lo++; hi--;
      } else if (s < 0) lo++;
      else hi--;
    }
  }
  return out;
}

Tradeoff: O(n^2) — sort is O(n log n), then n iterations of an O(n) two-pointer scan. The 'skip equal neighbors' pattern dedupes without a Set.

Plaid-specific tips

Plaid grades this on the dedup logic. The naive approach with a Set works but bloats memory. The skip-while-equal pattern is the standard. Bonus signal: connect this to their fraud-detection rule where three transactions that net to zero (debit + credit + fee=0) within a short window indicate a wash trade.

Common mistakes

  • Forgetting to skip duplicate i values — produces duplicate triplets.
  • Skipping duplicates in the wrong direction (e.g., `nums[lo] === nums[lo - 1]` immediately after lo++).
  • Not sorting first — two-pointer requires monotonicity.

Follow-up questions

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

  • 3Sum Closest (LC 16) — find triplet closest to a target.
  • 4Sum (LC 18) — outer i, inner j, then two-pointer.
  • kSum generalization.

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?

Sorting enables two-pointer scanning and makes dedup trivial (equal neighbors are adjacent). Without sort you'd need a hash-based dedup.

Why skip duplicates only after finding a match?

Different starting positions can match the same triplet. Skipping equal neighbors after pushing avoids re-emitting it.

Practice these live with InterviewChamp.AI

Drill 3Sum and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →