Array Sorting

Dutch National Flag Algorithm

An efficient, in-place sorting algorithm for arrays with three distinct values. This single-pass approach is a common interview question.

Sort Colors (0s, 1s, and 2s)

This implementation sorts an array containing only 0s, 1s, and 2s using the three-pointer Dutch National Flag approach.

function sortColors(nums) { let low = 0, mid = 0, high = nums.length - 1; while (mid <= high) { if (nums[mid] === 0) { [nums[low++], nums[mid++]] = [nums[mid], nums[low]]; } else if (nums[mid] === 1) { mid++; } else { [nums[mid], nums[high--]] = [nums[high], nums[mid]]; } } }
AI Coach Hint: The key is the invariant: all elements before `low` are 0s, all elements between `low` and `mid` are 1s, and all elements after `high` are 2s. The loop continues until `mid` crosses `high`, processing the unknown region.

Related Algorithm Guides

Explore more algorithm interview guides powered by AI coaching

Heap Vs Bst Interview Questions
AI-powered interview preparation guide
Topological Sort Interview Questions
AI-powered interview preparation guide
Object Oriented Programming Oop Interview Questions
AI-powered interview preparation guide
Site Reliability Engineer Interview Preparation
AI-powered interview preparation guide