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
MediumLC 213

House Robber II (LeetCode 213)

Target Frequency:GoogleMetaAmazon

Every house on this block stores a certain amount of cash, but the block forms a loop: house 0 sits right next to the final house, not just its immediate neighbors on either side. Any two neighboring houses share one alarm, so hitting both on the same night trips it.

Given the cash stashed in each house as nums, work out the largest total you can steal in one night while never picking two neighboring houses (remembering the first and last houses count as neighbors too).

Worked Examples

Example 1
Input:nums = [2,3,2]
Output:3
Explanation: Houses 1 and 3 sit next to each other on this circular block (house 3 loops back to house 1), so robbing both isn't allowed even though each holds 2.
Example 2
Input:nums = [1,2,3,1]
Output:4
Explanation: Choosing house 1 (worth 1) and house 3 (worth 3), which are not adjacent, nets 1 + 3 = 4 in total.
Example 3
Input:nums = [1,2,3]
Output:3
Explanation: The first and last houses are neighbours on the loop, so 1 and 3 cannot both be taken; robbing just the house with 3 is best.

⚖️Formal Constraints & Bounds

  • 1 <= nums.length <= 100

  • 0 <= nums[i] <= 1000

Deep-Dive & Conceptual Insights

Core Insight

Decomposing circular graphs into linear paths solves boundary constraints without custom edge logic.

Real-World Scenario & Production Applications

Ring topology network security scheduling, circular resource allocation.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Circular Constraint Decoupling

Because House 1 and House N are adjacent, robbing both is illegal. Decouple the circular graph into two independent linear subproblems: House 0 to N-2 (exclude last) and House 1 to N-1 (exclude first).

Mathematical Recurrence / Code Invariant
# Decouple into two linear runs
loot_1 = rob_linear(nums[:-1])  # Exclude last house
loot_2 = rob_linear(nums[1:])   # Exclude first house
return max(loot_1, loot_2)

Step-by-Step Execution Trace Table

SliceHouses EvaluatedLinear Max Money
Slice 1 (exclude last)[2, 3]3
Slice 2 (exclude first)[3, 2]3
Resultmax(3, 3)3
Code Shape
Archetype Code Shape
Python
1if len(nums) == 1:
2 return nums[0]
3def rob_linear(arr):
4 rob1, rob2 = 0, 0
5 for x in arr:
6 temp = max(rob2, rob1 + x)
7 rob1 = rob2
8 rob2 = temp
9 return rob2
10return max(rob_linear(nums[:-1]), rob_linear(nums[1:]))

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

In House Robber I, houses were arranged linearly: adjacent houses triggered alarms, leading to the state recurrence dp[i]=max⁡(dp[i−1],dp[i−2]+nums[i])dp[i] = \max(dp[i-1], dp[i-2] + nums[i])dp[i]=max(dp[i−1],dp[i−2]+nums[i]). In House Robber II, the houses are arranged in a circle, meaning the first house nums[0] and the last house nums[n-1] are neighbors. The core intuition is to resolve the cycle by decomposing the circular graph into two independent linear subproblems:

  1. Case A: Rob from House 000 to House n−2n-2n−2 (guarantees House n−1n-1n−1 is never touched).
  2. Case B: Rob from House 111 to House n−1n-1n−1 (guarantees House 000 is never touched). The optimal answer is strictly max⁡(Case A,Case B)\max(\text{Case A}, \text{Case B})max(Case A,Case B).
🏟️ The Analogy: A Circular Necklace with a Locked Clasp

Imagine a circular necklace of valuable gems. If you pry two adjacent gems loose, an alarm sounds. The clasp connects Gem 000 to Gem n−1n-1n−1. To remove the circular dependency:

  • You cut the clasp and forbid touching Gem n−1n-1n−1. Now it's a straight linear string of gems from 000 to n−2n-2n−2.
  • Separately, you forbid touching Gem 000. Now it's a straight linear string of gems from 111 to n−1n-1n−1. Since you can never legally take both Gem 000 and Gem n−1n-1n−1 together, one of these two linear cuts is guaranteed to contain the best possible total.
🔑 Why It Works

Notice how cleanly the circular constraint collapses: by running standard O(1)O(1)O(1)-space House Robber I twice across two array slices, we solve a circular dynamic program with zero additional state complexity in O(N)O(N)O(N) time.

  • Single element input trap: Running two sub-slices nums[:-1] and nums[1:] when len(nums) == 1. Both slices are empty ([]), causing the algorithm to return 0 instead of nums[0]. Guard if len(nums) == 1: return nums[0].

  • Attempting a three-way case split: Slices nums[:-1] (houses 0 to N−2N-2N−2) and nums[1:] (houses 1 to N−1N-1N−1) fully cover all valid possibilities, including robbing neither. A 3-way split adds unnecessary complexity.

  • Re-implementing linear robber twice: Copy-pasting the linear robber logic twice introduces silent divergence bugs; factor into a helper function rob_linear(sub_array).

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Circular adjacency constraints on linear dynamic programming problems.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: house 0 and house n-1 cannot be co-selected.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Complex circular edge tracking instead of clean array slicing.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"Since the first and last houses are adjacent, you cannot rob both. I run linear House Robber twice: once on nums[:-1] (excluding last) and once on nums[1:] (excluding first). The answer is the maximum of both. 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: Runs linear House Robber twice: once on nums[0..N-2] (ignoring last) and once on nums[1..N-1] (ignoring first). Takes max of both results. Total 2 * (N - 1) operations.

SPACE COMPLEXITY

O(1) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Stores 2 scalar variables per linear pass in O(1) space.

Formal Recurrence Relation

T(N) = 2 · O(N) = O(N)

Derivation Progression

Circular Constraint Decoupling

max(rob(0 dots N-2), rob(1 dots N-1))

First and last houses cannot both be robbed, breaking circular dependency.

Dual Linear Passes

2 × (N-1) transitions

Runs standard House Robber on each sub-array.

Linear Time Bound

O(N) total time

Constant factor of 2 over standard linear scan.

Variable Definitions

NNN

Total number of houses in the circular neighborhood

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(1) Scalar registers

🟢 Output Space

O(1) Max loot integer

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1) for N=1.

Average Case

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

Worst Case

O(N)O(N)O(N) general case.

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: "houses arranged in a circle", "first house is neighbor of last". Circular constraint reduction: house 0 and house N−1N-1N−1 are adjacent, so we can NEVER rob both. A Senior SWE decomposes the circle into two standard linear House Robber subproblems: 1. Rob houses [0…N−2][0 \dots N-2][0…N−2] (exclude last) 2. Rob houses [1…N−1][1 \dots N-1][1…N−1] (exclude first).

CONSTRAINTS & BOUNDS

N≤100N \le 100N≤100. Time budget O(N)O(N)O(N) linear time.

FAANG PRODUCTION TRAPS & EDGE CASES

Slicing an array of length 1 would pass empty arrays to the helper!

Theory Context•Dynamic Programming
MediumLC 213

House Robber II (LeetCode 213)

Target Frequency:GoogleMetaAmazon

Every house on this block stores a certain amount of cash, but the block forms a loop: house 0 sits right next to the final house, not just its immediate neighbors on either side. Any two neighboring houses share one alarm, so hitting both on the same night trips it.

Given the cash stashed in each house as nums, work out the largest total you can steal in one night while never picking two neighboring houses (remembering the first and last houses count as neighbors too).

Worked Examples

Example 1
Input:nums = [2,3,2]
Output:3
Explanation: Houses 1 and 3 sit next to each other on this circular block (house 3 loops back to house 1), so robbing both isn't allowed even though each holds 2.
Example 2
Input:nums = [1,2,3,1]
Output:4
Explanation: Choosing house 1 (worth 1) and house 3 (worth 3), which are not adjacent, nets 1 + 3 = 4 in total.
Example 3
Input:nums = [1,2,3]
Output:3
Explanation: The first and last houses are neighbours on the loop, so 1 and 3 cannot both be taken; robbing just the house with 3 is best.

⚖️Formal Constraints & Bounds

  • 1 <= nums.length <= 100

  • 0 <= nums[i] <= 1000

Deep-Dive & Conceptual Insights

Core Insight

Decomposing circular graphs into linear paths solves boundary constraints without custom edge logic.

Real-World Scenario & Production Applications

Ring topology network security scheduling, circular resource allocation.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Circular Constraint Decoupling

Because House 1 and House N are adjacent, robbing both is illegal. Decouple the circular graph into two independent linear subproblems: House 0 to N-2 (exclude last) and House 1 to N-1 (exclude first).

Mathematical Recurrence / Code Invariant
# Decouple into two linear runs
loot_1 = rob_linear(nums[:-1])  # Exclude last house
loot_2 = rob_linear(nums[1:])   # Exclude first house
return max(loot_1, loot_2)

Step-by-Step Execution Trace Table

SliceHouses EvaluatedLinear Max Money
Slice 1 (exclude last)[2, 3]3
Slice 2 (exclude first)[3, 2]3
Resultmax(3, 3)3
Code Shape
Archetype Code Shape
Python
1if len(nums) == 1:
2 return nums[0]
3def rob_linear(arr):
4 rob1, rob2 = 0, 0
5 for x in arr:
6 temp = max(rob2, rob1 + x)
7 rob1 = rob2
8 rob2 = temp
9 return rob2
10return max(rob_linear(nums[:-1]), rob_linear(nums[1:]))

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

In House Robber I, houses were arranged linearly: adjacent houses triggered alarms, leading to the state recurrence dp[i]=max⁡(dp[i−1],dp[i−2]+nums[i])dp[i] = \max(dp[i-1], dp[i-2] + nums[i])dp[i]=max(dp[i−1],dp[i−2]+nums[i]). In House Robber II, the houses are arranged in a circle, meaning the first house nums[0] and the last house nums[n-1] are neighbors. The core intuition is to resolve the cycle by decomposing the circular graph into two independent linear subproblems:

  1. Case A: Rob from House 000 to House n−2n-2n−2 (guarantees House n−1n-1n−1 is never touched).
  2. Case B: Rob from House 111 to House n−1n-1n−1 (guarantees House 000 is never touched). The optimal answer is strictly max⁡(Case A,Case B)\max(\text{Case A}, \text{Case B})max(Case A,Case B).
🏟️ The Analogy: A Circular Necklace with a Locked Clasp

Imagine a circular necklace of valuable gems. If you pry two adjacent gems loose, an alarm sounds. The clasp connects Gem 000 to Gem n−1n-1n−1. To remove the circular dependency:

  • You cut the clasp and forbid touching Gem n−1n-1n−1. Now it's a straight linear string of gems from 000 to n−2n-2n−2.
  • Separately, you forbid touching Gem 000. Now it's a straight linear string of gems from 111 to n−1n-1n−1. Since you can never legally take both Gem 000 and Gem n−1n-1n−1 together, one of these two linear cuts is guaranteed to contain the best possible total.
🔑 Why It Works

Notice how cleanly the circular constraint collapses: by running standard O(1)O(1)O(1)-space House Robber I twice across two array slices, we solve a circular dynamic program with zero additional state complexity in O(N)O(N)O(N) time.

  • Single element input trap: Running two sub-slices nums[:-1] and nums[1:] when len(nums) == 1. Both slices are empty ([]), causing the algorithm to return 0 instead of nums[0]. Guard if len(nums) == 1: return nums[0].

  • Attempting a three-way case split: Slices nums[:-1] (houses 0 to N−2N-2N−2) and nums[1:] (houses 1 to N−1N-1N−1) fully cover all valid possibilities, including robbing neither. A 3-way split adds unnecessary complexity.

  • Re-implementing linear robber twice: Copy-pasting the linear robber logic twice introduces silent divergence bugs; factor into a helper function rob_linear(sub_array).

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Circular adjacency constraints on linear dynamic programming problems.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: house 0 and house n-1 cannot be co-selected.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Complex circular edge tracking instead of clean array slicing.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"Since the first and last houses are adjacent, you cannot rob both. I run linear House Robber twice: once on nums[:-1] (excluding last) and once on nums[1:] (excluding first). The answer is the maximum of both. 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: Runs linear House Robber twice: once on nums[0..N-2] (ignoring last) and once on nums[1..N-1] (ignoring first). Takes max of both results. Total 2 * (N - 1) operations.

SPACE COMPLEXITY

O(1) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Stores 2 scalar variables per linear pass in O(1) space.

Formal Recurrence Relation

T(N) = 2 · O(N) = O(N)

Derivation Progression

Circular Constraint Decoupling

max(rob(0 dots N-2), rob(1 dots N-1))

First and last houses cannot both be robbed, breaking circular dependency.

Dual Linear Passes

2 × (N-1) transitions

Runs standard House Robber on each sub-array.

Linear Time Bound

O(N) total time

Constant factor of 2 over standard linear scan.

Variable Definitions

NNN

Total number of houses in the circular neighborhood

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(1) Scalar registers

🟢 Output Space

O(1) Max loot integer

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1) for N=1.

Average Case

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

Worst Case

O(N)O(N)O(N) general case.

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: "houses arranged in a circle", "first house is neighbor of last". Circular constraint reduction: house 0 and house N−1N-1N−1 are adjacent, so we can NEVER rob both. A Senior SWE decomposes the circle into two standard linear House Robber subproblems: 1. Rob houses [0…N−2][0 \dots N-2][0…N−2] (exclude last) 2. Rob houses [1…N−1][1 \dots N-1][1…N−1] (exclude first).

CONSTRAINTS & BOUNDS

N≤100N \le 100N≤100. Time budget O(N)O(N)O(N) linear time.

FAANG PRODUCTION TRAPS & EDGE CASES

Slicing an array of length 1 would pass empty arrays to the helper!

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