Two Pointers•Pattern: Two Pointers•Medium

Four Sum — find all unique quadruplets summing to target

Problem Intuition & Objective

Generalize 3Sum to four elements.

Time ComplexityO(n³)
Optimal
Space ComplexityO(1) extra
Memory Bounds
Optimal Algorithmic Approach

Sort + O(n²) outer loops + two-pointer O(n) inner.

Core Invariants & Edge Cases

  • Sort the array
  • Fix two outer loops (O(n²)) and use two-pointer for the inner pair
  • Skip duplicates at every pointer level
Previous ProblemLongest subarray with sum ≤ k (all positives)Next Problem Trapping rain water

System Design & Architecture Roadmap

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