Bird
Raised Fist0
Interview Prepdp-linear-fibonaccieasyAmazonAirbnb

House Robber

Choose your preparation mode4 modes available
📋
Problem

Imagine a professional thief planning to rob houses along a street. Each house has a certain amount of money, but robbing two adjacent houses triggers an alarm. How can the thief maximize the loot without alerting the police?

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police (i.e., you cannot rob two adjacent houses). Input: nums (list of integers). Output: integer representing maximum loot.

1 ≤ nums.length ≤ 10^50 ≤ nums[i] ≤ 10^4
Edge cases: Single house [5] → output 5All houses have zero money [0,0,0] → output 0Two houses with same money [10,10] → output 10
</>
IDE
def rob(nums: list[int]) -> int:public int rob(int[] nums)int rob(vector<int>& nums)function rob(nums)
def rob(nums):
    # Write your solution here
    pass
class Solution {
    public int rob(int[] nums) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int rob(vector<int>& nums) {
    // Write your solution here
    return 0;
}
function rob(nums) {
    // Write your solution here
}
Coming soon
0/9
Common Bugs to Avoid
Wrong: 7Greedy approach picking houses with local max values without considering adjacency.✅ Implement dp recurrence: dp[i] = max(dp[i-1], dp[i-2] + nums[i]) to consider all combinations.
Wrong: 11Robbing adjacent houses due to confusion with unbounded knapsack problem.✅ Enforce no two adjacent houses by using dp recurrence that skips adjacent indices.
Wrong: Error or non-zero on empty inputNo base case for empty input causing crash or wrong output.✅ Add base case: if nums is empty, return 0 immediately.
Wrong: 0Incorrect handling of single element input returning zero instead of nums[0].✅ Return nums[0] if length of nums is 1.
Wrong: 20Incorrect initialization of dp for two houses, summing both instead of max.✅ Initialize dp[1] = max(nums[0], nums[1]) to avoid robbing adjacent houses.
✓
Test Cases
t1_01basic
Input{"nums":[1,2,3,1]}
Expected4

Rob house 1 (money = 1) and house 3 (money = 3), total = 4.

t1_02basic
Input{"nums":[2,7,9,3,1]}
Expected12

Rob houses 1, 3, and 5 (2 + 9 + 1 = 12) for maximum loot.

t2_01edge
Input{"nums":[]}
Expected0

Empty list means no houses to rob, so max loot is 0.

t2_02edge
Input{"nums":[5]}
Expected5

Only one house to rob, so max loot is the value of that house.

t2_03edge
Input{"nums":[10,10]}
Expected10

Two houses with same money; can only rob one, so max loot is 10.

t3_01corner
Input{"nums":[1,2,3,1,2,3,1]}
Expected7

Rob houses 2, 4, and 6 (2 + 1 + 3 = 6) or 1, 3, 5, 7 (1 + 3 + 2 + 1 = 7), max is 8 by robbing houses 2, 4, 6.

t3_02corner
Input{"nums":[2,2,4,3,2,5]}
Expected11

Rob houses 1, 3, and 6 (2 + 4 + 5 = 11) but adjacent houses 3 and 4 conflict, so best is houses 2, 4, 6 (2 + 3 + 5 = 10).

t3_03corner
Input{"nums":[0,0,0,0]}
Expected0

All houses have zero money, so max loot is 0.

t4_01performance
Input{"nums":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}
⏱ Performance - must finish in 2000ms

n=100, O(n) DP must complete within 2 seconds.

Practice

(1/5)
1. Given the following code, what is the value of max_profit after the loop finishes when prices = [3, 2, 6]?
easy
A. 0
B. 3
C. 2
D. 4

Solution

  1. Step 1: Trace iteration i=1 with price=2

    min_price updated from 3 to 2, max_profit remains 0.
  2. Step 2: Trace iteration i=2 with price=6

    max_profit updated to max(0, 6 - 2) = 4.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Max profit is 4 from buying at 2 and selling at 6 [OK]
Hint: Update min price before max profit each iteration [OK]
Common Mistakes:
  • Confusing indices and off-by-one errors
  • Updating max profit before min price
  • Returning intermediate instead of final max profit
2. Consider the following Python code for maximizing stock profit with transaction fee:
def maxProfit(prices, fee):
    cash, hold = 0, -prices[0]
    for price in prices[1:]:
        cash = max(cash, hold + price - fee)
        hold = max(hold, cash - price)
    return cash

prices = [1, 3, 2]
fee = 1
print(maxProfit(prices, fee))
What is the output of this code?
easy
A. 1
B. 0
C. 3
D. 2

Solution

  1. Step 1: Initialize variables

    cash = 0, hold = -1 (buy at price 1)
  2. Step 2: Iterate over prices

    Price=3: cash = max(0, -1 + 3 - 1) = max(0,1) = 1; hold = max(-1, 1 - 3) = max(-1,-2) = -1 Price=2: cash = max(1, -1 + 2 - 1) = max(1,0) = 1; hold = max(-1, 1 - 2) = max(-1,-1) = -1
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Final cash is 1, representing max profit after fee [OK]
Hint: Track cash and hold updates carefully [OK]
Common Mistakes:
  • Off-by-one in loop
  • Forgetting fee subtraction
3. Consider the following Python code implementing the optimal solution for the House Robber II problem. What is the final output when the input is [2, 3, 2]?
def rob_linear(nums, start, end):
    prev, curr = 0, 0
    for i in range(start, end + 1):
        prev, curr = curr, max(curr, prev + nums[i])
    return curr

def rob(nums):
    n = len(nums)
    if n == 1:
        return nums[0]
    return max(rob_linear(nums, 0, n - 2), rob_linear(nums, 1, n - 1))

print(rob([2,3,2]))
easy
A. 4
B. 2
C. 3
D. 5

Solution

  1. Step 1: Trace rob_linear(nums, 0, 1)

    nums[0]=2, nums[1]=3 Iteration i=0: prev=0, curr=0 -> prev=0, curr=max(0,0+2)=2 Iteration i=1: prev=2, curr=max(2,0+3)=3 Return 3
  2. Step 2: Trace rob_linear(nums, 1, 2)

    nums[1]=3, nums[2]=2 Iteration i=1: prev=0, curr=0 -> prev=0, curr=max(0,0+3)=3 Iteration i=2: prev=3, curr=max(3,0+2)=3 Return 3 Max(3,3) = 3
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Both cases yield 3, final output is 3 [OK]
Hint: Trace both linear cases and pick max [OK]
Common Mistakes:
  • Off-by-one in loop range
  • Confusing which subarray to use
  • Incorrect max calculation
4. Given the following code for the Jump Game II problem, what is the value of the variable jumps returned when the input is [2,1,1]?
easy
A. 3
B. 2
C. 1
D. 0

Solution

  1. Step 1: Trace variables for input [2,1,1]

    n=3, loop i in [0,1]. - i=0: furthest = max(0, 0+2) = 2; i == current_end (0), so jumps = 1, current_end = 2 - i=1: furthest = max(2, 1+1) = 2; i != current_end (2), no jump increment Loop ends.
  2. Step 2: Return jumps

    jumps = 1 is returned, meaning minimum 1 jump to reach the end.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    One jump from index 0 to last index covers entire array [OK]
Hint: Increment jumps only when i reaches current_end [OK]
Common Mistakes:
  • Incrementing jumps too early or too late
  • Off-by-one errors in loop range
  • Misunderstanding when to update current_end
5. What is the time complexity of the optimal dynamic programming solution for the House Robber II problem using the space-optimized approach shown below?
def rob_linear(nums, start, end):
    prev, curr = 0, 0
    for i in range(start, end + 1):
        prev, curr = curr, max(curr, prev + nums[i])
    return curr

def rob(nums):
    n = len(nums)
    if n == 1:
        return nums[0]
    return max(rob_linear(nums, 0, n - 2), rob_linear(nums, 1, n - 1))
medium
A. O(n) with constant extra space
B. O(n²) due to nested loops
C. O(2^n) because of recursion without memoization
D. O(n) because each house is processed twice in two linear scans

Solution

  1. Step 1: Identify loops in rob_linear

    rob_linear runs a single loop from start to end, which is O(n) in worst case.
  2. Step 2: Count calls in rob

    rob calls rob_linear twice, each over roughly n/2 elements, so total O(n) time.
  3. Step 3: Consider space usage

    The DP uses only constant extra space (variables prev and curr) during computation.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Two linear scans over n elements with constant space -> O(n) time and O(1) space [OK]
Hint: Two linear scans sum to O(n) time with constant space [OK]
Common Mistakes:
  • Confusing recursion with DP
  • Assuming nested loops cause O(n²)
  • Ignoring that calls are sequential, not nested
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard