Pattern this teaches:1D Dynamic Programming
House Robber II
A medium Dynamic Programming interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Dynamic Programming
- Sheets
- 2
- Core for
- 9 roles
The problem
Houses stand in a circle, each holding some amount of money. You cannot take from two neighbouring houses, and because the street is a circle the first and last houses count as neighbours too. Return the largest total you can collect.
Example 1
- Input
- nums = [4,2,6,3,5]
- Output
- 11
- Why
- Exclude last: [4,2,6,3] -> 4+6=10. Exclude first: [2,6,3,5] -> 6+5=11. Max is 11.
Example 2
- Input
- nums = [3,7,2,5]
- Output
- 12
- Why
- Exclude last: [3,7,2] -> 7. Exclude first: [7,2,5] -> 7+5=12. Max is 12.
Example 3
- Input
- nums = [5,1,3,2]
- Output
- 8
- Why
- Exclude last: [5,1,3] -> 5+3=8. Exclude first: [1,3,2] -> 3+2=5. Max is 8.
Constraints
- 1 <= nums.length <= 100
- 0 <= nums[i] <= 1000
How to think about it
Updated 2026-09-29The circular connection only creates a conflict between the very first and very last house. You can never rob both in the same run. This splits the circular problem cleanly into two linear sub-problems: solve once excluding the last house, and once excluding the first house, then take the maximum.
Approaches, worst first
Recursive search with circular tracking
time O(2^n) · space O(n)
Branch recursively through the houses while passing a flag that records whether the first house was robbed to prevent robbing the last house. Recomputes overlapping states.
Two linear runsWrite this one
time O(n) · space O(1)
Reuse the linear House Robber routine twice: first on slice nums[0..n-2], and second on slice nums[1..n-1]. Each run runs in linear time using constant space, and the answer is the larger of the two results.
Where people lose marks · 3
- A single-element array has no distinct start and end. Slicing nums[0..n-2] produces an empty slice if n == 1. The edge case n == 1 must immediately return nums[0].
- Assuming that omitting the first house implies the last house MUST be robbed. The second linear pass allows the last house to be considered; it does not force it.
- Creating deep subarray copies when slicing. Passing index ranges to a helper function avoids allocating extra memory.
The theory behind it
Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems
What Dynamic Programming is
Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.
When to reach for it
Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.
How the pattern works
Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.
More Dynamic Programming problems
Target Roles
Core requirement for 9 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .