House Robber
Problem
You are given a list of non-negative integers nums, where nums[i] is the amount of value stored at position i along a street. You may collect from any subset of positions, with one restriction: you can never collect from two positions that are directly next to each other. Return the largest total you can collect.
- The list has between 1 and 5,000 positions.
- Each value is a non-negative integer no larger than 10,000.
Examples
nums = [3, 10, 4, 6, 8]→18. Take positions 1 and 4 (10 + 8). Taking10and4together is not allowed because they are adjacent.nums = [6, 1, 1, 6]→12. Take both ends; the two 1s in the middle are never worth displacing a 6.nums = [9]→9. A single position can always be taken.
Hints
- Consider the last position. Either you take it or you skip it. What does each choice force on the rest of the list?
- If you knew the best total achievable using only the first
i-1positions and only the firsti-2positions, could you decide positioniwithout looking further back? - This is a 1D DP with the recurrence
best[i] = max(best[i-1], best[i-2] + nums[i]), and it needs only two rolling values.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.