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•Dynamic Programming
HardLC 1879

Minimum XOR Sum of Two Arrays (LeetCode 1879)

Target Frequency:GoogleAmazonMicrosoft

Put nums2 in the order that makes its XOR sum with nums1 as small as possible, and return that smallest sum. nums1 keeps its order, and both arrays have the same length n.

The XOR sum of two arrays lined up position by position adds nums1[i] XOR nums2[i] over every position i. For instance, [1,2] against [3,1] gives (1 XOR 3) + (2 XOR 1) = 2 + 3 = 5.

Worked Examples

Example 1
Input:nums1 = [1,2], nums2 = [2,3]
Output:2
nums1
1021
nums2 reordered
3021
Explanation: Reordering nums2 to [3,2] pairs 1 with 3 (XOR 2) and 2 with 2 (XOR 0): 2. Keeping [2,3] gives 3 + 1 = 4.
Example 2
Input:nums1 = [1,0,3], nums2 = [5,3,4]
Output:8
nums1
100132
nums2 reordered
504132
Explanation: Pairing 1 with 5, 0 with 4 and 3 with 3 gives 4 + 4 + 0 = 8, and no order does better.

⚖️Formal Constraints & Bounds

  • n == nums1.length

  • n == nums2.length

  • 1 <= n <= 14

  • 0 <= nums1[i], nums2[i] <= 107

Deep-Dive & Conceptual Insights

Core Insight

The rest of the pairing only depends on which nums2 numbers are used, so one best total per set is enough, and the set's size tells which nums1 number comes next: n! orders collapse into 2^n masks.

Real-World Scenario & Production Applications

Assigning n tasks to n workers where each pairing has a cost is the classic assignment problem. When n is small and the cost is irregular, as with scheduling a handful of jobs on machines with setup costs or matching a few sensors to a few readings, one best cost per set of assigned workers finds the optimum without trying every order.

Step-by-Step Execution Trace Table

Example 1, nums1 = [1,2], nums2 = [2,3] (n = 2, four masks). Bit j of the mask means nums2[j] is paired:

Stepmaski = popcount(mask)Free jRelaxationdp after the step
Startdp = [0, inf, inf, inf][0, inf, inf, inf]
10000, 1dp[01] = 0 + (1 ^ 2) = 3; dp[10] = 0 + (1 ^ 3) = 2[0, 3, 2, inf]
20111dp[11] = 3 + (2 ^ 3) = 4[0, 3, 2, 4]
31010dp[11] = min(4, 2 + (2 ^ 2)) = 2[0, 3, 2, 2]
4112nonei == n: skip[0, 3, 2, 2]
Endreturn dp[11]2

At steps 2 and 3, i is 1 because one bit is set: nums1[0] is already paired, whichever nums2 number it took. Using any other nums1 index there would pair nums1[0] a second time.

Code Shape
Archetype Code Shape
Python
1Only the set of `nums2` numbers already paired matters, so keep one best total per set: `dp[mask]`, where bit `j` of `mask` means `nums2[j]` is used.
2From each `mask`, pair the next `nums1` number with every free `nums2[j]` and keep the smaller total in `dp[mask | (1 << j)]`.
3The shape: one table entry per mask, masks visited from small to large, the next `nums1` index read from the mask, an inner loop over the free `nums2` numbers, and the answer at the full mask.
4The trap: the next `nums1` index is `i = bin(mask).count("1")`, read from the mask. Pairing any index with the mask loses which `nums1` numbers are used.

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Trying every way to pair nums1 with nums2 means trying every order of nums2: n! of them, about 87 billion for n = 14. Bitmask DP notices that the future only depends on which nums2 numbers are already used, not on the order they were used in. So it keeps one best total per set, and writes the set as the bits of an integer, mask: 2^n states, about 16,000 for n = 14.

🎭 The Analogy: A Seating Chart

Guests arrive in a fixed order and each takes a free chair. To seat the next guest well, you don't need the story of who sat where first; you only need to know which chairs are taken. Many different histories leave the same chairs taken, and they all continue the same way, so you only keep the cheapest history for each set of taken chairs.

🔑 Why It Works

The mask carries everything: its number of set bits is how many nums1 numbers are paired, so nums1[i] with i = bin(mask).count("1") is the next one. Adding a number sets a bit and makes the integer larger, so a plain upward loop over mask reaches every state after all the states that lead into it.

  • Tracking the nums1 index yourself: i = bin(mask).count("1"). Pairing any nums1 index with the mask does not record which nums1 numbers are used, so one can be paired twice ([2,4,8] with [1,1,1] gives 9 instead of 17).

  • Dropping the parentheses in the bit test: write mask & (1 << j). In C++ or JavaScript, mask & 1 << j == 0 parses as mask & ((1 << j) == 0), which is always 0; in Java it does not compile.

  • Visiting masks out of order: loop mask from 0 upward and start every entry but dp[0] = 0 at infinity. mask | (1 << j) is always larger, so each state is final before it is read.

  • Adding a dimension for i: dp[i][mask] works but is n times larger for nothing: i is already the number of bits set in mask.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"Put nums2 in the order that makes its XOR sum with nums1 as small as possible", with 1 <= n <= 14: an optimum over orders (n! of them) where only the set of used items matters for the rest. A small n plus "which items are used" is the signal for Bitmask DP.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

dp[mask] is the least XOR sum after pairing the first i = bin(mask).count("1") numbers of nums1 with the nums2 numbers whose bits are set in mask. From each mask, for every free j (not mask & (1 << j)): dp[mask | (1 << j)] = min(dp[mask | (1 << j)], dp[mask] + (nums1[i] ^ nums2[j])). The answer is dp[(1 << n) - 1].

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Greedy pairing (each nums1 number takes the free nums2 number with the smallest XOR) fails: [0,3,6] with [4,4,2] gives 11 instead of 7.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I'd use Bitmask DP. Trying every order of nums2 is n factorial, but the rest of the problem only depends on which nums2 numbers are already used, so I keep one best total per set, written as the bits of an integer mask. I pair nums1 in order, so the next nums1 index is just the number of bits set in the mask. From each mask I try every free nums2 number, add their XOR, and keep the smaller total in the larger mask. Setting a bit always makes the number bigger, so looping masks upward reads each state after everything that leads into it. The trap is keeping the nums1 index separately: then the mask no longer says which nums1 numbers are used, and one can be paired twice. The answer is the full mask. That's O(N times 2 to the N) time and O(2 to the N) space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N * 2^N)

Look at the code: for mask in range(1 << n) runs 2^N times. Inside, bin(mask).count("1") is O(N) and for j in range(n) runs N times with an O(1) bit test, OR and min. Total: 2^N * O(N) = O(N * 2^N).

SPACE COMPLEXITY

O(2^N)

The code keeps one list, dp, with one entry per mask: 2^N entries, plus a few integers. The answer is one integer.

Formal Recurrence Relation

T(N)=2N⋅(O(N)+N⋅O(1))=O(N⋅2N)T(N) = 2^N \cdot (O(N) + N \cdot O(1)) = O(N \cdot 2^N)T(N)=2N⋅(O(N)+N⋅O(1))=O(N⋅2N)

Derivation Progression

States

2^N

for mask in range(1 << n) visits every subset of nums2 once, smallest integer first.

Next index

O(N) per mask

i = bin(mask).count("1") counts the set bits.

Transitions

N per mask

for j in range(n) tests each bit with mask & (1 << j) and relaxes dp[mask | (1 << j)] in O(1).

Total

O(N * 2^N)

2^N masks times O(N) work each; for N = 14 that is about 2.3 * 10^5 steps instead of 14! = 8.7 * 10^10 orders.

Variable Definitions

NNN

Length of nums1 and nums2

Memory Architecture & Bounds

🟣 Call Stack

O(1): iterative, no recursion

🔵 Auxiliary Heap

O(2^N): the dp table

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(N⋅2N)O(N \cdot 2^N)O(N⋅2N): every mask is visited whatever the numbers are

Average Case

O(N⋅2N)O(N \cdot 2^N)O(N⋅2N)

Worst Case

O(N⋅2N)O(N \cdot 2^N)O(N⋅2N)

State-Space & Subproblem DAG(Directed Acyclic Graph) Topology

State-Space & Subproblem DAG(Directed Acyclic Graph) Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "put nums2 in the order that makes its XOR sum with nums1 as small as possible", "return that smallest sum", and 1 <= n <= 14. An optimum over orders where only the set of used items matters: Bitmask DP, one state per set.

CONSTRAINTS & BOUNDS

N≤14N \le 14N≤14: N!≈8.7×1010N! \approx 8.7 \times 10^{10}N!≈8.7×1010 orders is far too many, while 214=16,3842^{14} = 16{,}384214=16,384 masks with 14 tries each is about 2.3×1052.3 \times 10^52.3×105 steps. Values up to 10710^7107 keep every XOR below 2242^{24}224 and the total below 2.4×1082.4 \times 10^82.4×108, inside 32 bits.

FAANG PRODUCTION TRAPS & EDGE CASES

The table has 2N2^N2N entries, so memory, not time, is usually the first limit: at N=20N = 20N=20 it is a million entries, at N=30N = 30N=30 a billion; past about 20 items the idea needs meet-in-the-middle or a different model.

Theory Context•Dynamic Programming
HardLC 1879

Minimum XOR Sum of Two Arrays (LeetCode 1879)

Target Frequency:GoogleAmazonMicrosoft

Put nums2 in the order that makes its XOR sum with nums1 as small as possible, and return that smallest sum. nums1 keeps its order, and both arrays have the same length n.

The XOR sum of two arrays lined up position by position adds nums1[i] XOR nums2[i] over every position i. For instance, [1,2] against [3,1] gives (1 XOR 3) + (2 XOR 1) = 2 + 3 = 5.

Worked Examples

Example 1
Input:nums1 = [1,2], nums2 = [2,3]
Output:2
nums1
1021
nums2 reordered
3021
Explanation: Reordering nums2 to [3,2] pairs 1 with 3 (XOR 2) and 2 with 2 (XOR 0): 2. Keeping [2,3] gives 3 + 1 = 4.
Example 2
Input:nums1 = [1,0,3], nums2 = [5,3,4]
Output:8
nums1
100132
nums2 reordered
504132
Explanation: Pairing 1 with 5, 0 with 4 and 3 with 3 gives 4 + 4 + 0 = 8, and no order does better.

⚖️Formal Constraints & Bounds

  • n == nums1.length

  • n == nums2.length

  • 1 <= n <= 14

  • 0 <= nums1[i], nums2[i] <= 107

Deep-Dive & Conceptual Insights

Core Insight

The rest of the pairing only depends on which nums2 numbers are used, so one best total per set is enough, and the set's size tells which nums1 number comes next: n! orders collapse into 2^n masks.

Real-World Scenario & Production Applications

Assigning n tasks to n workers where each pairing has a cost is the classic assignment problem. When n is small and the cost is irregular, as with scheduling a handful of jobs on machines with setup costs or matching a few sensors to a few readings, one best cost per set of assigned workers finds the optimum without trying every order.

Step-by-Step Execution Trace Table

Example 1, nums1 = [1,2], nums2 = [2,3] (n = 2, four masks). Bit j of the mask means nums2[j] is paired:

Stepmaski = popcount(mask)Free jRelaxationdp after the step
Startdp = [0, inf, inf, inf][0, inf, inf, inf]
10000, 1dp[01] = 0 + (1 ^ 2) = 3; dp[10] = 0 + (1 ^ 3) = 2[0, 3, 2, inf]
20111dp[11] = 3 + (2 ^ 3) = 4[0, 3, 2, 4]
31010dp[11] = min(4, 2 + (2 ^ 2)) = 2[0, 3, 2, 2]
4112nonei == n: skip[0, 3, 2, 2]
Endreturn dp[11]2

At steps 2 and 3, i is 1 because one bit is set: nums1[0] is already paired, whichever nums2 number it took. Using any other nums1 index there would pair nums1[0] a second time.

Code Shape
Archetype Code Shape
Python
1Only the set of `nums2` numbers already paired matters, so keep one best total per set: `dp[mask]`, where bit `j` of `mask` means `nums2[j]` is used.
2From each `mask`, pair the next `nums1` number with every free `nums2[j]` and keep the smaller total in `dp[mask | (1 << j)]`.
3The shape: one table entry per mask, masks visited from small to large, the next `nums1` index read from the mask, an inner loop over the free `nums2` numbers, and the answer at the full mask.
4The trap: the next `nums1` index is `i = bin(mask).count("1")`, read from the mask. Pairing any index with the mask loses which `nums1` numbers are used.

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Trying every way to pair nums1 with nums2 means trying every order of nums2: n! of them, about 87 billion for n = 14. Bitmask DP notices that the future only depends on which nums2 numbers are already used, not on the order they were used in. So it keeps one best total per set, and writes the set as the bits of an integer, mask: 2^n states, about 16,000 for n = 14.

🎭 The Analogy: A Seating Chart

Guests arrive in a fixed order and each takes a free chair. To seat the next guest well, you don't need the story of who sat where first; you only need to know which chairs are taken. Many different histories leave the same chairs taken, and they all continue the same way, so you only keep the cheapest history for each set of taken chairs.

🔑 Why It Works

The mask carries everything: its number of set bits is how many nums1 numbers are paired, so nums1[i] with i = bin(mask).count("1") is the next one. Adding a number sets a bit and makes the integer larger, so a plain upward loop over mask reaches every state after all the states that lead into it.

  • Tracking the nums1 index yourself: i = bin(mask).count("1"). Pairing any nums1 index with the mask does not record which nums1 numbers are used, so one can be paired twice ([2,4,8] with [1,1,1] gives 9 instead of 17).

  • Dropping the parentheses in the bit test: write mask & (1 << j). In C++ or JavaScript, mask & 1 << j == 0 parses as mask & ((1 << j) == 0), which is always 0; in Java it does not compile.

  • Visiting masks out of order: loop mask from 0 upward and start every entry but dp[0] = 0 at infinity. mask | (1 << j) is always larger, so each state is final before it is read.

  • Adding a dimension for i: dp[i][mask] works but is n times larger for nothing: i is already the number of bits set in mask.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"Put nums2 in the order that makes its XOR sum with nums1 as small as possible", with 1 <= n <= 14: an optimum over orders (n! of them) where only the set of used items matters for the rest. A small n plus "which items are used" is the signal for Bitmask DP.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

dp[mask] is the least XOR sum after pairing the first i = bin(mask).count("1") numbers of nums1 with the nums2 numbers whose bits are set in mask. From each mask, for every free j (not mask & (1 << j)): dp[mask | (1 << j)] = min(dp[mask | (1 << j)], dp[mask] + (nums1[i] ^ nums2[j])). The answer is dp[(1 << n) - 1].

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Greedy pairing (each nums1 number takes the free nums2 number with the smallest XOR) fails: [0,3,6] with [4,4,2] gives 11 instead of 7.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I'd use Bitmask DP. Trying every order of nums2 is n factorial, but the rest of the problem only depends on which nums2 numbers are already used, so I keep one best total per set, written as the bits of an integer mask. I pair nums1 in order, so the next nums1 index is just the number of bits set in the mask. From each mask I try every free nums2 number, add their XOR, and keep the smaller total in the larger mask. Setting a bit always makes the number bigger, so looping masks upward reads each state after everything that leads into it. The trap is keeping the nums1 index separately: then the mask no longer says which nums1 numbers are used, and one can be paired twice. The answer is the full mask. That's O(N times 2 to the N) time and O(2 to the N) space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N * 2^N)

Look at the code: for mask in range(1 << n) runs 2^N times. Inside, bin(mask).count("1") is O(N) and for j in range(n) runs N times with an O(1) bit test, OR and min. Total: 2^N * O(N) = O(N * 2^N).

SPACE COMPLEXITY

O(2^N)

The code keeps one list, dp, with one entry per mask: 2^N entries, plus a few integers. The answer is one integer.

Formal Recurrence Relation

T(N)=2N⋅(O(N)+N⋅O(1))=O(N⋅2N)T(N) = 2^N \cdot (O(N) + N \cdot O(1)) = O(N \cdot 2^N)T(N)=2N⋅(O(N)+N⋅O(1))=O(N⋅2N)

Derivation Progression

States

2^N

for mask in range(1 << n) visits every subset of nums2 once, smallest integer first.

Next index

O(N) per mask

i = bin(mask).count("1") counts the set bits.

Transitions

N per mask

for j in range(n) tests each bit with mask & (1 << j) and relaxes dp[mask | (1 << j)] in O(1).

Total

O(N * 2^N)

2^N masks times O(N) work each; for N = 14 that is about 2.3 * 10^5 steps instead of 14! = 8.7 * 10^10 orders.

Variable Definitions

NNN

Length of nums1 and nums2

Memory Architecture & Bounds

🟣 Call Stack

O(1): iterative, no recursion

🔵 Auxiliary Heap

O(2^N): the dp table

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(N⋅2N)O(N \cdot 2^N)O(N⋅2N): every mask is visited whatever the numbers are

Average Case

O(N⋅2N)O(N \cdot 2^N)O(N⋅2N)

Worst Case

O(N⋅2N)O(N \cdot 2^N)O(N⋅2N)

State-Space & Subproblem DAG(Directed Acyclic Graph) Topology

State-Space & Subproblem DAG(Directed Acyclic Graph) Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "put nums2 in the order that makes its XOR sum with nums1 as small as possible", "return that smallest sum", and 1 <= n <= 14. An optimum over orders where only the set of used items matters: Bitmask DP, one state per set.

CONSTRAINTS & BOUNDS

N≤14N \le 14N≤14: N!≈8.7×1010N! \approx 8.7 \times 10^{10}N!≈8.7×1010 orders is far too many, while 214=16,3842^{14} = 16{,}384214=16,384 masks with 14 tries each is about 2.3×1052.3 \times 10^52.3×105 steps. Values up to 10710^7107 keep every XOR below 2242^{24}224 and the total below 2.4×1082.4 \times 10^82.4×108, inside 32 bits.

FAANG PRODUCTION TRAPS & EDGE CASES

The table has 2N2^N2N entries, so memory, not time, is usually the first limit: at N=20N = 20N=20 it is a million entries, at N=30N = 30N=30 a billion; past about 20 items the idea needs meet-in-the-middle or a different model.

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