The House Robber II interview question is a direct sequel to the original House Robber problem. The premise is the same: you want to maximize your loot from houses along a street without robbing adjacent ones. However, there is a major twist: the street is a circle. This means the first house and the last house are now adjacent, and robbing both will trigger the alarm.
Companies like Meta, Amazon, and Uber use this problem to see if a candidate can adapt a known solution to a new constraint. It evaluations your ability to handle circular dependencies in Dynamic Programming. The challenge lies in breaking the circle into linear components, demonstrating that you can simplify complex structures into manageable sub-problems.
This problem uses the Linear DP pattern from the first House Robber problem, applied twice. Since you cannot rob both the first and the last house, the solution must be the maximum of two scenarios:
Houses: [2, 3, 2]
[2, 3]. Standard House Robber result = 3.[3, 2]. Standard House Robber result = 3.
Max of both is 3. (Note: if it were linear, you could get 4 by robbing the two 2s).Houses: [1, 2, 3, 1]
[1, 2, 3]. Result = 4.[2, 3, 1]. Result = 4.
Max result: 4.Always look for ways to "break" circularity. Most circular array or string problems can be solved by either concatenating the array to itself or, as in this case, running a linear algorithm on two different segments.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Partition Equal Subset Sum | Medium | Solve | |
| House Robber | Medium | Solve | |
| Coin Change II | Medium | Solve | |
| Best Time to Buy and Sell Stock with Cooldown | Medium | Solve | |
| Maximum Product Subarray | Medium | Solve |