Hi👋SpeedAlgo • Deliberate Practice & Cognitive Ergonomics for Software Engineers

An interactive algorithm mastery and technical interview preparation platform published by Hi👋WebEnterprise. Built for senior and staff software engineers preparing for rigorous coding screens at top tech companies (FAANG/MAMAA).

13 Core Algorithmic Patterns & 225 Practice Problems

  • 1. Two Pointers (16 Paradigms, 43 Problems): Opposite ends, sorted pair sums, palindrome verification, trapping rain water, 3Sum, container with most water — includes the Sliding Window and Fast & Slow Pointers paradigms (contiguous subarray invariants, longest substrings, Floyd's cycle detection, linked list middle).
  • 2. Binary Search (9 Paradigms, 14 Problems): Monotonic predicate partitioning (feasible(x)), boundary search, rotated sorted arrays, square roots, minimum ship capacity, median of two sorted arrays, matrix median on value range.
  • 3. Bit Manipulation (5 Paradigms, 11 Problems): Counting set bits, XOR cancellation, bit reversal, bitmask pairing, maximum XOR with a bitwise trie.
  • 4. Math & Geometry (8 Paradigms, 14 Problems): Sieve of Eratosthenes, integer/roman conversions, modular arithmetic, geometric simulation.
  • 5. Tree/Graph Depth-First Search (15 Paradigms, 24 Problems): Path sums, lowest common ancestor, tree diameter, validating BSTs, tree DP (House Robber III, Binary Tree Cameras).
  • 6. Tree/Graph Breadth-First Search (5 Paradigms, 12 Problems): Level-order traversals, shortest path in unweighted graphs, zig-zag traversals, rotting oranges, word ladders.
  • 7. Graphs (12 Paradigms, 22 Problems): Topological sort, cycle detection, Dijkstra's algorithm, bipartite graph validation, network delay.
  • 8. Backtracking (6 Paradigms, 14 Problems): Subsets, permutations, combinations, constraint satisfaction, pruning, N-Queens, Sudoku solver.
  • 9. Dynamic Programming (13 Paradigms, 28 Problems): Memoization vs tabulation, 0/1 knapsack, unbounded knapsack, coin change, edit distance, longest common subsequence.
  • 10. Heap / Priority Queue (9 Paradigms, 11 Problems): Running medians, top-k elements, interval scheduling, IPO, k-way merges.
  • 11. Advanced Data Structures (7 Paradigms, 17 Problems): Trie (prefix tree), Union-Find (Disjoint Set Union), LRU Cache, LFU Cache, range-sum queries with updates.
  • 12. Stack (5 Paradigms, 9 Problems): Next greater element, largest rectangle in histogram, remove k digits, decode string, min stack.
  • 13. Intervals & Greedy (2 Paradigms, 6 Problems): Merge intervals, interval scheduling, meeting rooms by sweep line, jump game.

4-Stage Deliberate Practice Framework

  1. Stage 1 (Compare & Learn): Read the pattern's mental model, loop invariant and Big-O proof, compare the pattern's template with the problem's solution side by side, and use the step-by-step debugger. The solution can also be shown in C#, Java, TypeScript, C++, Go or Rust for almost every problem.
  2. Stage 2 (Active Recall): Rebuild the solution from memory in a code editor, with an invariant checklist and optional progressive hints, and check it against a judged test suite.
  3. Stage 3 (AI Interview Coach, coming soon): A mock-interview stage still in development; for now, students go straight from Stage 2 to Stage 4.
  4. Stage 4 (Solve on Your Own): Solve a new problem on your own against a 15-minute timer; your code is graded against a test suite. Python and TypeScript run in your browser and C# runs on our server (design problems grade in Python and TypeScript).

Equipped with a spaced-repetition review hub on a fixed 7-step schedule (1 day up to 90 days), a two-pane Studio workspace, and study notes.

Pricing, Access & Commercial Terms

  • Core Curriculum: 100% Free. No credit card required.
  • Coins: New accounts get 40 free coins once, the daily claim adds +20 coins once a day, and inviting a friend earns you +25 coins when they complete their first practice step. These are the only ways to earn coins.
  • AI Coach: Each AI coach question costs 1-5 coins, priced by the server according to its size.
  • BYOK (Bring Your Own Key): AI coaching is free with your own Google Gemini, OpenAI or OpenRouter API key (BYOK).
  • Extras: Coins also unlock extras for 24 hours: a Recall hint level (1 coin), a Solve solution reveal (2), a LeetCode problem card's solutions (1) and a pattern's extra canonical problems (1).
  • Payments & Refunds: Nothing on the site is sold for money: coins can only be earned, never bought. There are no subscriptions or charges, so there is nothing to refund.
  • Platform Operator: Hi👋WebEnterprise. Support & policies at hispeedalgo.com.
Skip to main content
Hi👋SpeedAlgo

Invariant-First Algorithmic Mastery

Arena
238Items
Theory Context•Two Pointers & Sliding Window
MediumLC 75

Sort Colors (LeetCode 75)

Target Frequency:MetaAmazonMicrosoft

You have an array nums of n objects, each colored red, white, or blue. Rearrange them in-place so same-colored objects sit next to each other, ordered red, then white, then blue.

The colors are encoded as integers: 0 for red, 1 for white, and 2 for blue.

You may not call a built-in sort function to solve this.

Worked Examples

Example 1
Input:nums = [2,0,2,1,1,0]
Output:[0,0,1,1,2,2]
Explanation: 0s are placed at the front, 1s in the middle, and 2s at the end.
Example 2
Input:nums = [2,0,1]
Output:[0,1,2]
Explanation: One 0, one 1 and one 2, arranged red, white, blue.

⚖️Formal Constraints & Bounds

  • n == nums.length

  • 1 <= n <= 300

  • nums[i] is either 0, 1, or 2.

Deep-Dive & Conceptual Insights

Core Insight

By maintaining three pointers (low for 0-boundary, mid for unexamined elements, and high for 2-boundary), each element is placed into its correct region in a single pass without needing extra memory.

Real-World Scenario & Production Applications

Three-way quicksort partitioning, multi-category triage buffers, and hardware memory tiering.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Three-Region Pointer Setup (Dutch National Flag)

low marks the boundary of the 0-region, mid scans unexamined elements, and high marks the boundary of the 2-region. Everything before low is 0, everything after high is 2, and mid walks the unknown middle.

Mathematical Recurrence / Code Invariant
low, mid, high = 0, 0, len(nums) - 1

Step-by-Step Execution Trace Table

Steplowmidhighnums[mid]Actionnums State
00052nums[mid]==2 →\rightarrow→ swap with high, high--[0, 0, 2, 1, 1, 2]
10040nums[mid]==0 →\rightarrow→ swap with low, low++, mid++[0, 0, 2, 1, 1, 2]
21140nums[mid]==0 →\rightarrow→ swap with low, low++, mid++[0, 0, 2, 1, 1, 2]
32242nums[mid]==2 →\rightarrow→ swap with high, high--[0, 0, 1, 1, 2, 2]
42231nums[mid]==1 →\rightarrow→ mid++[0, 0, 1, 1, 2, 2]
52331nums[mid]==1 →\rightarrow→ mid++[0, 0, 1, 1, 2, 2]
End243—mid > high →\rightarrow→ loop terminates[0, 0, 1, 1, 2, 2]
Code Shape
Archetype Code Shape
Python
1low, mid, high = 0, 0, len(nums) - 1
2while mid <= high:
3 if nums[mid] == 0:
4 swap(low, mid)
5 low += 1
6 mid += 1
7 elif nums[mid] == 1:
8 mid += 1
9 else:
10 swap(mid, high)
11 high -= 1
12return nums

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Move Zeroes partitioned elements into two regions (0 vs non-zero) using two pointers (reader and writer). In Sort Colors, there are three distinct categories (0, 1, and 2), requiring Dijkstra's Dutch National Flag algorithm with three pointers working in synergy.

🎨 The Analogy: A Three-Bin Conveyor Belt

Picture a conveyor belt carrying red (0), white (1), and blue (2) balls:

  • At the far left sits the Red Bin (low pointer).
  • At the far right sits the Blue Bin (high pointer).
  • The center zone between low and mid is reserved for White balls.
  • An inspector (mid) examines each ball:
    1. If Red (0): Toss it into the left bin (swap(low, mid)), then advance both low and mid. (The item swapped from low is guaranteed to be 1, so mid can safely move on).
    2. If White (1): It's already in the middle territory! Advance mid.
    3. If Blue (2): Toss it into the right bin (swap(mid, high)), and decrement high. Crucial: Do NOT advance mid! The ball that just arrived from high was unexamined—it might be red, white, or blue. You must inspect it on the next step.
  • Advancing mid pointer after swapping with high: When nums[mid] == 2 and you swap with nums[high], the incoming element at mid from high has never been inspected (it could be 0, 1, or 2). You must decrement high but keep mid stationary.

  • Terminating loop with < instead of <=: The Dutch National Flag loop must execute while mid <= high:. If you terminate at mid < high, the element at high is never inspected and remains unsorted.

  • Swapping with low without advancing both pointers: When nums[mid] == 0, swapping with nums[low] guarantees the incoming element at mid is 1 (because low <= mid and all values before mid are already 0 or 1). Thus both low += 1 and mid += 1 must advance.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Three-category partition + in-place requirement + single-pass constraint.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: nums[0..low-1] == 0, nums[low..mid-1] == 1, nums[high+1..n-1] == 2, with nums[mid..high] unexamined.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

This problem's traps are in Common Pitfalls above.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I maintain three pointers: low, mid, and high. If nums[mid] is 0, I swap nums[low] and nums[mid], incrementing both low and mid. If nums[mid] is 1, I just increment mid. If nums[mid] is 2, I swap nums[mid] and nums[high] and decrement high without incrementing mid. Runs in O(N) time and O(1) space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the execution steps: Each iteration of the while loop either increments mid or decrements high. Since high - mid decreases by at least 1 at every step, the loop executes at most N times.

SPACE COMPLEXITY

O(1) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Uses only three scalar pointer variables (low, mid, high) in-place.

Formal Recurrence Relation

T(N) = O(N)

Derivation Progression

Pointer Range Shrinkage

high−mid decreases each stephigh - mid \text{ decreases each step}high−mid decreases each step

Each iteration either advances mid by 1 (when 0 or 1) or retreats high by 1 (when 2).

Constant Work Per Step

O(1) swap and pointer updateO(1) \text{ swap and pointer update}O(1) swap and pointer update

At most one two-element swap and scalar increment/decrement per iteration.

Total Work

≤N iterations×O(1)=O(N)\le N \text{ iterations} \times O(1) = O(N)≤N iterations×O(1)=O(N)

The loop terminates when mid > high, guaranteeing at most N total iterations.

Variable Definitions

NNN

Total number of elements in the array

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(1) Three scalar pointers

🟢 Output Space

O(1) In-place array modification

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N)

Average Case

O(N)O(N)O(N)

Worst Case

O(N)O(N)O(N)

Pointer Invariant Transition Progression

Pointer Invariant Transition Progression
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "sort array with 0s, 1s, 2s", "in-place", "one-pass algorithm using O(1) space". Dutch National Flag (Dijkstra) 3-way partition: partitions array into three distinct contiguous segments in a single pass.

CONSTRAINTS & BOUNDS

N≤300N \le 300N≤300, values ∈{0,1,2}\in \{0, 1, 2\}∈{0,1,2}. Single-pass O(N)O(N)O(N) time with O(1)O(1)O(1) auxiliary memory.

FAANG PRODUCTION TRAPS & EDGE CASES

THE CRITICAL TRAP: Incrementing mid after swapping with high! When swapping nums[mid] with nums[high], the element brought in from high was previously uninspected; mid must NOT advance, allowing the next iteration to evaluate it. Conversely, swapping with low is guaranteed to bring in a 1 (since [low..mid-1] is all 1s), so advancing both low and mid is strictly safe.

Theory Context•Two Pointers & Sliding Window
MediumLC 75

Sort Colors (LeetCode 75)

Target Frequency:MetaAmazonMicrosoft

You have an array nums of n objects, each colored red, white, or blue. Rearrange them in-place so same-colored objects sit next to each other, ordered red, then white, then blue.

The colors are encoded as integers: 0 for red, 1 for white, and 2 for blue.

You may not call a built-in sort function to solve this.

Worked Examples

Example 1
Input:nums = [2,0,2,1,1,0]
Output:[0,0,1,1,2,2]
Explanation: 0s are placed at the front, 1s in the middle, and 2s at the end.
Example 2
Input:nums = [2,0,1]
Output:[0,1,2]
Explanation: One 0, one 1 and one 2, arranged red, white, blue.

⚖️Formal Constraints & Bounds

  • n == nums.length

  • 1 <= n <= 300

  • nums[i] is either 0, 1, or 2.

Deep-Dive & Conceptual Insights

Core Insight

By maintaining three pointers (low for 0-boundary, mid for unexamined elements, and high for 2-boundary), each element is placed into its correct region in a single pass without needing extra memory.

Real-World Scenario & Production Applications

Three-way quicksort partitioning, multi-category triage buffers, and hardware memory tiering.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Three-Region Pointer Setup (Dutch National Flag)

low marks the boundary of the 0-region, mid scans unexamined elements, and high marks the boundary of the 2-region. Everything before low is 0, everything after high is 2, and mid walks the unknown middle.

Mathematical Recurrence / Code Invariant
low, mid, high = 0, 0, len(nums) - 1

Step-by-Step Execution Trace Table

Steplowmidhighnums[mid]Actionnums State
00052nums[mid]==2 →\rightarrow→ swap with high, high--[0, 0, 2, 1, 1, 2]
10040nums[mid]==0 →\rightarrow→ swap with low, low++, mid++[0, 0, 2, 1, 1, 2]
21140nums[mid]==0 →\rightarrow→ swap with low, low++, mid++[0, 0, 2, 1, 1, 2]
32242nums[mid]==2 →\rightarrow→ swap with high, high--[0, 0, 1, 1, 2, 2]
42231nums[mid]==1 →\rightarrow→ mid++[0, 0, 1, 1, 2, 2]
52331nums[mid]==1 →\rightarrow→ mid++[0, 0, 1, 1, 2, 2]
End243—mid > high →\rightarrow→ loop terminates[0, 0, 1, 1, 2, 2]
Code Shape
Archetype Code Shape
Python
1low, mid, high = 0, 0, len(nums) - 1
2while mid <= high:
3 if nums[mid] == 0:
4 swap(low, mid)
5 low += 1
6 mid += 1
7 elif nums[mid] == 1:
8 mid += 1
9 else:
10 swap(mid, high)
11 high -= 1
12return nums

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Move Zeroes partitioned elements into two regions (0 vs non-zero) using two pointers (reader and writer). In Sort Colors, there are three distinct categories (0, 1, and 2), requiring Dijkstra's Dutch National Flag algorithm with three pointers working in synergy.

🎨 The Analogy: A Three-Bin Conveyor Belt

Picture a conveyor belt carrying red (0), white (1), and blue (2) balls:

  • At the far left sits the Red Bin (low pointer).
  • At the far right sits the Blue Bin (high pointer).
  • The center zone between low and mid is reserved for White balls.
  • An inspector (mid) examines each ball:
    1. If Red (0): Toss it into the left bin (swap(low, mid)), then advance both low and mid. (The item swapped from low is guaranteed to be 1, so mid can safely move on).
    2. If White (1): It's already in the middle territory! Advance mid.
    3. If Blue (2): Toss it into the right bin (swap(mid, high)), and decrement high. Crucial: Do NOT advance mid! The ball that just arrived from high was unexamined—it might be red, white, or blue. You must inspect it on the next step.
  • Advancing mid pointer after swapping with high: When nums[mid] == 2 and you swap with nums[high], the incoming element at mid from high has never been inspected (it could be 0, 1, or 2). You must decrement high but keep mid stationary.

  • Terminating loop with < instead of <=: The Dutch National Flag loop must execute while mid <= high:. If you terminate at mid < high, the element at high is never inspected and remains unsorted.

  • Swapping with low without advancing both pointers: When nums[mid] == 0, swapping with nums[low] guarantees the incoming element at mid is 1 (because low <= mid and all values before mid are already 0 or 1). Thus both low += 1 and mid += 1 must advance.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Three-category partition + in-place requirement + single-pass constraint.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: nums[0..low-1] == 0, nums[low..mid-1] == 1, nums[high+1..n-1] == 2, with nums[mid..high] unexamined.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

This problem's traps are in Common Pitfalls above.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I maintain three pointers: low, mid, and high. If nums[mid] is 0, I swap nums[low] and nums[mid], incrementing both low and mid. If nums[mid] is 1, I just increment mid. If nums[mid] is 2, I swap nums[mid] and nums[high] and decrement high without incrementing mid. Runs in O(N) time and O(1) space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the execution steps: Each iteration of the while loop either increments mid or decrements high. Since high - mid decreases by at least 1 at every step, the loop executes at most N times.

SPACE COMPLEXITY

O(1) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Uses only three scalar pointer variables (low, mid, high) in-place.

Formal Recurrence Relation

T(N) = O(N)

Derivation Progression

Pointer Range Shrinkage

high−mid decreases each stephigh - mid \text{ decreases each step}high−mid decreases each step

Each iteration either advances mid by 1 (when 0 or 1) or retreats high by 1 (when 2).

Constant Work Per Step

O(1) swap and pointer updateO(1) \text{ swap and pointer update}O(1) swap and pointer update

At most one two-element swap and scalar increment/decrement per iteration.

Total Work

≤N iterations×O(1)=O(N)\le N \text{ iterations} \times O(1) = O(N)≤N iterations×O(1)=O(N)

The loop terminates when mid > high, guaranteeing at most N total iterations.

Variable Definitions

NNN

Total number of elements in the array

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(1) Three scalar pointers

🟢 Output Space

O(1) In-place array modification

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N)

Average Case

O(N)O(N)O(N)

Worst Case

O(N)O(N)O(N)

Pointer Invariant Transition Progression

Pointer Invariant Transition Progression
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "sort array with 0s, 1s, 2s", "in-place", "one-pass algorithm using O(1) space". Dutch National Flag (Dijkstra) 3-way partition: partitions array into three distinct contiguous segments in a single pass.

CONSTRAINTS & BOUNDS

N≤300N \le 300N≤300, values ∈{0,1,2}\in \{0, 1, 2\}∈{0,1,2}. Single-pass O(N)O(N)O(N) time with O(1)O(1)O(1) auxiliary memory.

FAANG PRODUCTION TRAPS & EDGE CASES

THE CRITICAL TRAP: Incrementing mid after swapping with high! When swapping nums[mid] with nums[high], the element brought in from high was previously uninspected; mid must NOT advance, allowing the next iteration to evaluate it. Conversely, swapping with low is guaranteed to bring in a 1 (since [low..mid-1] is all 1s), so advancing both low and mid is strictly safe.

© 2026 Hi👋WebEnterprise. All rights reserved.
Sitemap•llms.txt•