House Robber II

Algorithms
Medium
Microsoft
133.2K views

You are a professional robber planning to rob houses along a street. This time, all houses are arranged in a circle. You cannot rob adjacent houses. Find the maximum amount you can rob.

Why Interviewers Ask This

Microsoft asks House Robber II to evaluate a candidate's ability to decompose circular dependency problems into solvable linear subproblems. Interviewers specifically test if you recognize that the circular constraint forces a choice between two scenarios: excluding the first house or excluding the last. This assesses your pattern recognition skills and your capacity to adapt standard dynamic programming solutions to modified constraints without overcomplicating the logic.

How to Answer This Question

1. Clarify Constraints: Immediately acknowledge the circular arrangement means the first and last houses are adjacent, preventing both from being robbed simultaneously. 2. Decompose Strategy: Propose splitting the problem into two distinct linear scenarios. Case A assumes you rob the first house (excluding the last), and Case B assumes you do not rob the first house (allowing the possibility of robbing the last). 3. Apply Standard DP: For each case, utilize the classic House Robber I recurrence relation where dp[i] equals the maximum of robbing the current house plus the previous non-adjacent total, or skipping it. 4. Handle Edge Cases: Explicitly address arrays with one or two elements to prevent index errors. 5. Synthesize Result: Return the maximum value derived from Case A and Case B. This structured breakdown demonstrates Microsoft's preference for clear, modular thinking and rigorous edge-case handling in algorithmic design.

Key Points to Cover

  • Explicitly stating the decomposition into two linear scenarios to handle the circular constraint
  • Demonstrating knowledge of the standard linear DP recurrence relation
  • Addressing edge cases like single-element or two-element arrays immediately
  • Ensuring the solution maintains O(n) time complexity and O(1) space complexity
  • Clearly articulating why the first and last houses cannot be robbed simultaneously

Sample Answer

To solve House Robber II, I first recognize that the circular layout introduces a critical constraint: we cannot rob both the first and the last house because they are neighbors. A direct application of the linear dynamic programming approach fails here because the boundary condition is ambiguous. To resolve this, I will decompose the problem into two separate linear sub-problems. First, I calculate the maximum amount possible by robbing houses from index 0 to n-2, effectively treating the array as linear while ignoring the last house. Second, I calculate the maximum amount by robbing houses from index 1 to n-1, which allows us to potentially rob the last house but strictly forbids robbing the first. By running the standard linear DP logic on these two specific ranges, we cover all valid configurations. The final answer is simply the maximum of these two results. For example, if the input is [2, 3, 2], the first scenario yields 2 (robbing index 0) and the second yields 3 (robbing index 1), so the answer is 3. This approach ensures O(n) time complexity and O(1) space complexity, adhering to efficiency standards expected at Microsoft. It also demonstrates my ability to handle circular dependencies by breaking them into manageable, linear components.

Common Mistakes to Avoid

  • Attempting to use a single DP pass with complex modulo arithmetic instead of splitting the problem
  • Forgetting to handle the edge case where the input array contains only one element
  • Implementing a recursive solution without memoization leading to Time Limit Exceeded errors
  • Overlooking that robbing the first house automatically disqualifies the last house from being selected

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 65 Microsoft questions