Sorting•Pattern: Three Pointers / Partition•Medium

Sort colors (Dutch National Flag)

Problem Intuition & Objective

Sort an array of 0s, 1s, and 2s in-place in O(n) with one pass.

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

Dutch National Flag: partition into three regions.

Core Invariants & Edge Cases

  • Three pointers: low, mid, high
  • 0 → swap to low, advance low and mid
  • 2 → swap to high, decrement high; 1 → just advance mid
Previous ProblemMerge intervalsNext Problem Longest increasing subsequence (LIS)

System Design & Architecture Roadmap

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