Reported September 2026
Amazondynamic programming

House Robber

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this Amazon House Robber question, reported in September 2026, is going greedy. Grab the biggest house, skip its neighbors, move on. It looks right and fails on inputs like [2,7,9,3,1] if you pick wrong early. This is a classic dynamic programming problem on a flat array, and the whole thing fits in a few lines once you see the recurrence. If you blank mid-assessment, StealthCoder is the invisible safety net that reads the problem on screen and gives you the solution live. But you should know the trick going in.

The problem

You are given an array nums, where nums[i] is the amount of money in house i.
Adjacent houses cannot both be robbed. Return the maximum total amount that can be robbed without choosing two adjacent houses.

Function
rob(nums: int[]) → int

Examples
Example 1
nums = [1,2,3,1]
return = 4
Rob houses 0 and 2 for 1 + 3 = 4.
Example 2
nums = [2,7,9,3,1]
return = 12
Rob houses 0, 2, and 4 for 2 + 9 + 1 = 12.
Example 3
nums = [5]
return = 5
The only house can be robbed.

Constraints
1 <= nums.length <= 100.
0 <= nums[i] <= 400.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a two-choice recurrence. At each house i, you either skip it and keep the best from i-1, or rob it and add nums[i] to the best from i-2. So best[i] = max(best[i-1], best[i-2] + nums[i]). You only need the last two values, so keep two variables and run in O(n) time and O(1) space. The common pitfall is greedy picking, or alternating houses (odd vs even) and taking the larger sum. Both break when the best answer skips two houses in a row, like robbing 0 and 3 in a longer array. Also watch the single-house case: nums = [5] must return 5, so initialize your variables so a length-1 array works without indexing past the end. If the live OA freezes you up, StealthCoder is the hedge that gets you the working recurrence fast.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill House Robber cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as house robber. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

House Robber FAQ

How hard is the Amazon House Robber question really?+

It's an easy-to-medium DP problem. The input is a flat array of up to 100 values, so there are no tricky edge structures. If you know the take-or-skip recurrence, it's about ten lines. The difficulty is spotting that greedy fails and DP is needed.

What's the trick to solving House Robber?+

At each house, choose the max of skipping it (keep the previous best) or robbing it (previous-previous best plus this house). Track two rolling variables instead of a full table. That's the whole solution, O(n) time and O(1) space.

Why doesn't the greedy approach work?+

Picking the largest house first can block two better neighbors. In [2,7,9,3,1], the answer 12 comes from houses 0, 2, and 4, not from chasing the single biggest value. Greedy has no way to compare the sum of alternatives, which is what DP does.

What edge cases should I test?+

Test a single house like [5], which should return 5. Test two houses, where you return the larger one. Test zeros, since values can be 0. Make sure your initialization doesn't read nums[1] when the array has only one element.

How do I prepare for this in 48 hours?+

Write the recurrence from memory twice, once with a dp array and once with two variables. Then trace [1,2,3,1] and [2,7,9,3,1] by hand and confirm you get 4 and 12. Then try the circular-street variant to see how the same idea adapts.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it