‹ DS&A interview · Socratic
DSA · Two pointers · #12

3Sum

Module 15 · difficulty 3/5·30:00starts on first keystroke

Given an integer array `nums`, return all unique triplets `[nums[i], nums[j], nums[k]]` such that i, j, k are distinct indices and nums[i] + nums[j] + nums[k] == 0. The solution set must not contain duplicate triplets. Order of triplets and of values within a triplet does not matter.

Examples
  • nums = [-1,0,1,2,-1,-4] [[-1,-1,2],[-1,0,1]]
  • nums = [0,1,1] []no triplet sums to 0
  • nums = [0,0,0] [[0,0,0]]
Constraints
  • · 3 <= nums.length <= 3000
  • · -10^5 <= nums[i] <= 10^5
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.