Reported May 2024
Navandynamic programming

House Robber

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

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

The Navan OA reported in May 2024 is House Robber, and the trap is the greedy instinct. Grab the biggest house, skip its neighbors, repeat. It looks right and it fails. Take [2,5,1,3]. Greedy grabs 5, then 3, and happens to land on 8. Then [4,1,1,9,1] shows the real shape: the answer is 4 plus 9, not just the biggest value. This is a dynamic programming problem with two running numbers and no array needed. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the recurrence in real time. Better to know it cold first.

The problem

You are given an integer array nums. The value nums[i] is the amount available in the ith house on a street.
You may choose any set of houses, but you cannot choose two adjacent houses.
Return the maximum total amount you can collect.

Function
rob(nums: int[]) → int

Examples
Example 1
nums = [2,5,1,3]
return = 8
Choose the houses with amounts 5 and 3. They are not adjacent, and their total is 8.
Example 2
nums = [4,1,1,9,1]
return = 13
Choose the first house with amount 4 and the fourth house with amount 9 for a total of 13.

Constraints
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^4
The maximum total fits in a signed int.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is one decision per house: skip it or rob it. Let best[i] be the max through house i. Then best[i] = max(best[i-1], best[i-2] + nums[i]). You only need the last two values, so keep prev and curr and roll them forward. That gives O(n) time and O(1) space. The edge case that breaks naive code is length 1. If you index nums[1] or seed with two houses, you crash or return the wrong answer. Start both rolling values at 0 and loop from the first element, and it handles one house for free. The other pitfall is greedy by largest value, which fails on cases like [2,7,9,3,1] style inputs where the neighbors of a big house sum higher. With n up to 10^5, recursion without memoization times out and deep recursion can overflow the stack. Go iterative. StealthCoder is the hedge if the recurrence slips away under the clock.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

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 Navan's OA.

Navan reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

House Robber FAQ

How hard is the Navan House Robber question really?+

It's a standard one-dimensional DP problem. If you've seen the take-or-skip recurrence, it's ten lines. If you haven't, the hard part is spotting that greedy fails. Once you write best[i] = max(best[i-1], best[i-2] + nums[i]), the rest is mechanical.

What's the trick to solving House Robber?+

At each house, choose between skipping it and keeping the previous best, or robbing it and adding the best from two houses back. Take the max. Track only two variables instead of a full array, and you get O(n) time with O(1) space.

Why does the greedy approach fail here?+

Picking the largest house first can block two better neighbors. In [4,1,1,9,1] you want 4 and 9 for 13, and a locally greedy choice can miss combinations like that on other inputs. The decision at each house depends on earlier results, which is what DP captures.

What edge cases should I test before submitting?+

Test a single-element array, since length 1 is allowed. Test all zeros, since values can be 0. Test two elements, where you just take the larger. Also try a long array near 10^5 to confirm you're iterative and not hitting recursion depth limits.

How do I prepare for this in 48 hours?+

Write the recurrence from scratch three times, first with an array, then with two variables. Then do the circular variant, House Robber II, since it's a common follow-up. Trace the two examples by hand so the skip-or-take logic sticks.

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

OA at Navan?
Invisible during screen share
Get it