Reported May 2024
Navandynamic programming

House Robber II

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 hands you House Robber II, and the trap is sitting in the first sentence: the houses are in a circle. If you copy the linear House Robber DP and submit, [2,3,2] returns 4 instead of 3. That's the edge case that breaks the naive solution. It's a dynamic programming problem with one clean twist, and you can solve it in a few minutes once you see it. If you blank on the twist during the live assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the solution in real time.

The problem

Houses stand in a circle. The nonnegative integer nums[i] is the value available in house i. Select houses with no two adjacent and return the maximum total value.
The first and last houses are adjacent. For this exercise, assume there is at least one house, selecting none is allowed, and a single house may be selected.

Function
robCircular(nums: int[]) → int

Examples
Example 1
nums = [2,3,2]
return = 3
The two houses worth 2 are adjacent around the circle, so choose the middle house worth 3.
Example 2
nums = [1,2,3,1]
return = 4
Choose values 1 and 3 at indices 0 and 2; they are not adjacent.
Example 3
nums = [5]
return = 5
The only house may be selected.

Constraints
1 <= nums.length <= 10^5.
0 <= nums[i] <= 10^4.
The result fits in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: in a circle, house 0 and house n-1 can't both be picked. So run the linear House Robber twice. Once on nums[0..n-2], once on nums[1..n-1]. Return the max of the two. Each pass uses two variables, prev and curr, updated as curr = max(curr, prev + x). That's O(n) time and O(1) space, which matters with n up to 10^5. The pitfall is the single house case. With n = 1, both slices are empty, so you must return nums[0] directly. Example 3 tests exactly this. Values are nonnegative, so zero is a safe base and no negative handling is needed. Another mistake is trying to track a flag for whether house 0 was taken, which gets messy. Two clean passes beat it. If the recurrence slips under pressure, StealthCoder is the hedge during the live OA, reading the problem and handing you the two-pass structure.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill House Robber II 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as house robber ii. 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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

House Robber II FAQ

What's the trick in House Robber II?+

The circle means the first and last houses can't both be chosen. Solve the linear House Robber on everything except the last house, then on everything except the first, and take the larger result. Two linear passes cover every valid selection.

How hard is this really for the Navan OA?+

Medium. If you already know the linear House Robber recurrence, this is a small extension. The difficulty is spotting the circular constraint and handling the one-house case. Most failures come from skipping that edge case, not from the DP itself.

What edge case fails most solutions?+

A single house. Slicing nums[0..n-2] and nums[1..n-1] gives two empty ranges when n is 1, so you'd return 0 instead of 5 for [5]. Add an early return of nums[0] when the length is 1. Example 3 checks this directly.

Do I need an array for the DP table?+

No. Each step only depends on the previous two values, so keep two variables. Update with curr = max(curr, prev + x). That gives O(1) extra space and handles n up to 10^5 without any memory concern.

How do I prepare for this in 48 hours?+

Write the linear House Robber from memory, then wrap it in a helper that takes a range. Call it twice and take the max. Test with [2,3,2], [1,2,3,1], and [5]. That covers the logic and both edge cases you'll see.

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