Array•Pattern: Two Pointers•Medium

3Sum — find all unique triplets that sum to zero

Problem Intuition & Objective

Return all unique triplets [a, b, c] such that a + b + c = 0.

Time ComplexityO(n²)
Optimal
Space ComplexityO(1) (excluding output)
Memory Bounds
Optimal Algorithmic Approach

Sort + Two-Pointer: O(n²) overall, skipping duplicate values at each pointer to avoid duplicate triplets.

Core Invariants & Edge Cases

  • Sort the array first
  • Fix one element, then use two-pointer on the remaining sorted segment
  • Skip duplicates carefully for the fixed element and both pointers
Previous ProblemMaximum subarray sum (Kadane's Algorithm)Next Problem Container with Most Water

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.