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 2161

Partition Array According to Given Pivot (LeetCode 2161)

Target Frequency:AmazonGoogle

You have a 0-indexed integer array nums and an integer pivot. Reorder nums so that:

  1. Everything smaller than pivot sits before everything larger than pivot.
  2. Everything equal to pivot sits between the smaller and larger groups.
  3. Within the smaller group and within the larger group, the elements keep their original relative order.

Return nums once it's been reordered this way.

Worked Examples

Example 1
Input:nums = [9,12,5,10,14,3,10], pivot = 10
Output:[9,5,3,10,10,12,14]
Explanation: Elements less than 10 are 9, 5, and 3. Elements greater are 12 and 14. Elements equal are 10 and 10. Relative order is preserved.
Example 2
Input:nums = [-3,4,3,2], pivot = 2
Output:[-3,2,4,3]
Explanation: Elements less than 2 is -3. Elements greater are 4 and 3. Element equal is 2.

⚖️Formal Constraints & Bounds

  • 1 <= nums.length <= 105

  • -106 <= nums[i] <= 106

  • pivot equals to an element of nums

Deep-Dive & Conceptual Insights

Core Insight

Scanning forward places elements < pivot in original relative order at the front, scanning backward places elements > pivot in original relative order at the back, and the remaining middle slots are filled with pivot.

Real-World Scenario & Production Applications

Stable sorting passes, database query partition by boundary with stable tie-breaking, and audio buffer sorting.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Left Pass -- Stable Less-Than Prefix

Scan nums left to right, writing every element strictly less than pivot into a new result array in its original relative order, advancing a left write cursor.

Mathematical Recurrence / Code Invariant
res = [0] * len(nums)
left, right = 0, len(nums) - 1
for num in nums:
    if num < pivot:
        res[left] = num
        left += 1

Step-by-Step Execution Trace Table

PassPointer / IndexActionres State
Left Passi = 0..n-1If nums[i] < pivot: res[left++] = nums[i]Left prefix filled with [9, 5, 3] (left = 3)
Right Passi = n-1..0If nums[i] > pivot: res[right--] = nums[i]Right suffix filled with [12, 14] (right = 4)
Middle Fillleft..rightWhile left <= right: res[left++] = pivotMiddle slots filled with [10, 10]
Final—All three partitions stably assembled[9, 5, 3, 10, 10, 12, 14]
Code Shape
Archetype Code Shape
Python
1res = [0] * len(nums)
2left = 0
3right = len(nums) - 1
4for i in range(len(nums)):
5 if nums[i] < pivot:
6 res[left] = nums[i]
7 left += 1
8 if nums[len(nums)-1-i] > pivot:
9 res[right] = nums[len(nums)-1-i]
10 right -= 1
11while left <= right:
12 res[left] = pivot
13 left += 1
14return res

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Sort Colors (LC 75) could swap elements freely because all elements of the same color are identical. But in LC 2161, elements have distinct values and must maintain their original relative order (stability). Swapping across boundaries destroys stability!

⚖️ The Analogy: Two Pens Writing Inward

Imagine two scribes writing into a fresh output array res of size NNN:

  • The Left Scribe (left = 0) writes from the start, moving forward.
  • The Right Scribe (right = N - 1) writes from the end, moving backward.

You walk through the original array:

  1. Scanning forward from index 0 to N-1: whenever you see an element < pivot, the Left Scribe writes it at res[left++]. Because you scan forward, smaller elements are naturally placed in their exact original relative order!
  2. Scanning backward (N-1-i): whenever you see an element > pivot, the Right Scribe writes it at res[right--]. Because you read backward and write backward, larger elements are also placed in their exact original relative order!
  3. What is left between left and right? Exactly the slots reserved for pivot! Fill that gap with pivot.
  • Violating relative order stability: Using QuickSort-style in-place two-pointer partitioning scrambles the relative order of elements smaller or larger than pivot. LC 2161 strictly requires maintaining input relative order.

  • Allocating three separate passes unnecessarily: A clean two-pointer approach writes smaller elements from the left and larger elements from the right of the result buffer in a single pass.

  • Miscounting pivot frequency: Elements equal to pivot must appear between the smaller and larger partitions; forgetting to track the pivot count produces misaligned partitions.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Three-way partitioning + strict stability (relative order preservation) requirement.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: res[0..left-1] has all elements < pivot seen so far; res[right+1..n-1] has all elements > pivot in order.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Attempting in-place swaps like Dutch National Flag: breaks relative order (stability) of elements.

  • Using three separate lists and concatenating: incurs high memory allocation and reallocation overhead instead of a preallocated fixed-size result array.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I preallocate a result array of length N. In a single loop from 0 to N-1, I check nums[i] to place elements < pivot at left++, and simultaneously check nums[N-1-i] to place elements > pivot at right--. Finally, I fill remaining indices between left and right with pivot. Runs in O(N) time and O(N) auxiliary space while guaranteeing stability."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the execution steps: One linear pass of length N inspects elements from both ends and writes into the result array. The second fill loop fills the middle pivot elements in at most N steps. Total time is linear.

SPACE COMPLEXITY

O(N) Auxiliary

Look at memory allocations: A preallocated result array of size N is required to preserve stability. Pointers left and right use O(1) scalars.

Formal Recurrence Relation

T(N) = O(N)

Derivation Progression

Symmetric Scan

N iterationsN \text{ iterations}N iterations

Inspects nums[i] and nums[N - 1 - i], placing < pivot at left and > pivot at right.

Middle Pivot Fill

≤N iterations\le N \text{ iterations}≤N iterations

Fills index range [left..right] with pivot value.

Total Work

O(N) + O(N) = O(N)

Exact 2N operations, running in strict linear time.

Variable Definitions

NNN

Total number of elements in the array

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(N) Preallocated result array

🟢 Output Space

O(N) Output array of size N

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: "elements less than pivot appear before elements equal to pivot", "relative order must be preserved". The requirement of stability makes in-place O(1)O(1)O(1) space Dutch National Flag unsuitable. An optimal linear approach uses a dual-pointer fill into an auxiliary result array.

CONSTRAINTS & BOUNDS

N≤105N \le 10^5N≤105. Time budget O(N)O(N)O(N). Space budget O(N)O(N)O(N) auxiliary array for stable placement.

FAANG PRODUCTION TRAPS & EDGE CASES

Scanning forward for >pivot> pivot>pivot would reverse their relative order if writing from right; must scan nums backwards when writing >pivot> pivot>pivot to right from end down to middle.

Theory Context•Two Pointers & Sliding Window
MediumLC 2161

Partition Array According to Given Pivot (LeetCode 2161)

Target Frequency:AmazonGoogle

You have a 0-indexed integer array nums and an integer pivot. Reorder nums so that:

  1. Everything smaller than pivot sits before everything larger than pivot.
  2. Everything equal to pivot sits between the smaller and larger groups.
  3. Within the smaller group and within the larger group, the elements keep their original relative order.

Return nums once it's been reordered this way.

Worked Examples

Example 1
Input:nums = [9,12,5,10,14,3,10], pivot = 10
Output:[9,5,3,10,10,12,14]
Explanation: Elements less than 10 are 9, 5, and 3. Elements greater are 12 and 14. Elements equal are 10 and 10. Relative order is preserved.
Example 2
Input:nums = [-3,4,3,2], pivot = 2
Output:[-3,2,4,3]
Explanation: Elements less than 2 is -3. Elements greater are 4 and 3. Element equal is 2.

⚖️Formal Constraints & Bounds

  • 1 <= nums.length <= 105

  • -106 <= nums[i] <= 106

  • pivot equals to an element of nums

Deep-Dive & Conceptual Insights

Core Insight

Scanning forward places elements < pivot in original relative order at the front, scanning backward places elements > pivot in original relative order at the back, and the remaining middle slots are filled with pivot.

Real-World Scenario & Production Applications

Stable sorting passes, database query partition by boundary with stable tie-breaking, and audio buffer sorting.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Left Pass -- Stable Less-Than Prefix

Scan nums left to right, writing every element strictly less than pivot into a new result array in its original relative order, advancing a left write cursor.

Mathematical Recurrence / Code Invariant
res = [0] * len(nums)
left, right = 0, len(nums) - 1
for num in nums:
    if num < pivot:
        res[left] = num
        left += 1

Step-by-Step Execution Trace Table

PassPointer / IndexActionres State
Left Passi = 0..n-1If nums[i] < pivot: res[left++] = nums[i]Left prefix filled with [9, 5, 3] (left = 3)
Right Passi = n-1..0If nums[i] > pivot: res[right--] = nums[i]Right suffix filled with [12, 14] (right = 4)
Middle Fillleft..rightWhile left <= right: res[left++] = pivotMiddle slots filled with [10, 10]
Final—All three partitions stably assembled[9, 5, 3, 10, 10, 12, 14]
Code Shape
Archetype Code Shape
Python
1res = [0] * len(nums)
2left = 0
3right = len(nums) - 1
4for i in range(len(nums)):
5 if nums[i] < pivot:
6 res[left] = nums[i]
7 left += 1
8 if nums[len(nums)-1-i] > pivot:
9 res[right] = nums[len(nums)-1-i]
10 right -= 1
11while left <= right:
12 res[left] = pivot
13 left += 1
14return res

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Sort Colors (LC 75) could swap elements freely because all elements of the same color are identical. But in LC 2161, elements have distinct values and must maintain their original relative order (stability). Swapping across boundaries destroys stability!

⚖️ The Analogy: Two Pens Writing Inward

Imagine two scribes writing into a fresh output array res of size NNN:

  • The Left Scribe (left = 0) writes from the start, moving forward.
  • The Right Scribe (right = N - 1) writes from the end, moving backward.

You walk through the original array:

  1. Scanning forward from index 0 to N-1: whenever you see an element < pivot, the Left Scribe writes it at res[left++]. Because you scan forward, smaller elements are naturally placed in their exact original relative order!
  2. Scanning backward (N-1-i): whenever you see an element > pivot, the Right Scribe writes it at res[right--]. Because you read backward and write backward, larger elements are also placed in their exact original relative order!
  3. What is left between left and right? Exactly the slots reserved for pivot! Fill that gap with pivot.
  • Violating relative order stability: Using QuickSort-style in-place two-pointer partitioning scrambles the relative order of elements smaller or larger than pivot. LC 2161 strictly requires maintaining input relative order.

  • Allocating three separate passes unnecessarily: A clean two-pointer approach writes smaller elements from the left and larger elements from the right of the result buffer in a single pass.

  • Miscounting pivot frequency: Elements equal to pivot must appear between the smaller and larger partitions; forgetting to track the pivot count produces misaligned partitions.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Three-way partitioning + strict stability (relative order preservation) requirement.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: res[0..left-1] has all elements < pivot seen so far; res[right+1..n-1] has all elements > pivot in order.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Attempting in-place swaps like Dutch National Flag: breaks relative order (stability) of elements.

  • Using three separate lists and concatenating: incurs high memory allocation and reallocation overhead instead of a preallocated fixed-size result array.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I preallocate a result array of length N. In a single loop from 0 to N-1, I check nums[i] to place elements < pivot at left++, and simultaneously check nums[N-1-i] to place elements > pivot at right--. Finally, I fill remaining indices between left and right with pivot. Runs in O(N) time and O(N) auxiliary space while guaranteeing stability."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the execution steps: One linear pass of length N inspects elements from both ends and writes into the result array. The second fill loop fills the middle pivot elements in at most N steps. Total time is linear.

SPACE COMPLEXITY

O(N) Auxiliary

Look at memory allocations: A preallocated result array of size N is required to preserve stability. Pointers left and right use O(1) scalars.

Formal Recurrence Relation

T(N) = O(N)

Derivation Progression

Symmetric Scan

N iterationsN \text{ iterations}N iterations

Inspects nums[i] and nums[N - 1 - i], placing < pivot at left and > pivot at right.

Middle Pivot Fill

≤N iterations\le N \text{ iterations}≤N iterations

Fills index range [left..right] with pivot value.

Total Work

O(N) + O(N) = O(N)

Exact 2N operations, running in strict linear time.

Variable Definitions

NNN

Total number of elements in the array

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(N) Preallocated result array

🟢 Output Space

O(N) Output array of size N

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: "elements less than pivot appear before elements equal to pivot", "relative order must be preserved". The requirement of stability makes in-place O(1)O(1)O(1) space Dutch National Flag unsuitable. An optimal linear approach uses a dual-pointer fill into an auxiliary result array.

CONSTRAINTS & BOUNDS

N≤105N \le 10^5N≤105. Time budget O(N)O(N)O(N). Space budget O(N)O(N)O(N) auxiliary array for stable placement.

FAANG PRODUCTION TRAPS & EDGE CASES

Scanning forward for >pivot> pivot>pivot would reverse their relative order if writing from right; must scan nums backwards when writing >pivot> pivot>pivot to right from end down to middle.

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