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 this buggy code snippet for maxProfit with at most two transactions. Which line contains the subtle bug causing incorrect profit calculation?
medium
A. Line 10: Inner loop iterating forwards instead of backwards
B. Line 7: Initialization of maxDiff array
C. Line 9: Outer loop iterating from 1 to n
D. Line 6: Initialization of profit array

Solution

  1. Step 1: Identify iteration order in inner loop

    The inner loop iterates k from 1 to 2 (forwards). This causes profit[k-1] to be updated before profit[k], leading to incorrect maxDiff updates.
  2. Step 2: Correct iteration order

    Iterating k backwards (from 2 down to 1) ensures profit[k-1] is from previous iteration, preserving correct state transitions.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Backward iteration prevents premature state overwriting [OK]
Hint: Inner loop must run backwards to avoid overwriting states [OK]
Common Mistakes:
  • Iterating inner loop forwards
  • Incorrect maxDiff updates
3. What is the time and space complexity of the optimal space-optimized bottom-up DP solution for the Best Time -- Cooldown problem with input size n (number of days)?
medium
A. Time: O(n), Space: O(1)
B. Time: O(2^n), Space: O(n)
C. Time: O(n), Space: O(n)
D. Time: O(n²), Space: O(n)

Solution

  1. Step 1: Identify time complexity

    The algorithm iterates once through prices array, performing constant work per iteration -> O(n) time.
  2. Step 2: Identify space complexity

    Only constant variables (hold, cool, free) are used, no arrays -> O(1) space.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Single pass, constant variables -> O(n) time and O(1) space [OK]
Hint: One pass with constant states means O(n) time, O(1) space [OK]
Common Mistakes:
  • Confusing recursion stack space with iterative space
  • Assuming DP arrays used in bottom-up
4. The following code attempts to compute the maximum profit with transaction fee but contains a subtle bug:
def maxProfit(prices, fee):
    cash, hold = 0, -prices[0]
    for price in prices[1:]:
        cash = max(cash, hold + price)  # Bug here: fee not subtracted
        hold = max(hold, cash - price)
    return cash
What is the bug in this code?
medium
A. The hold update uses cash before it is updated in the same iteration.
B. The initial hold value should be zero instead of -prices[0].
C. The fee is not subtracted when selling stock, causing overestimated profit.
D. The loop should start from prices[0], not prices[1:].

Solution

  1. Step 1: Identify where fee is applied

    The fee must be subtracted when selling stock, i.e., when updating cash.
  2. Step 2: Locate bug in code

    cash update line adds hold + price but misses subtracting fee, leading to inflated profit.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Missing fee subtraction causes incorrect profit calculation [OK]
Hint: Fee must be subtracted exactly when selling [OK]
Common Mistakes:
  • Forgetting fee on sell
  • Incorrect initial hold value
5. The following code attempts to solve the Word Break problem. Identify the line that contains a subtle bug that can cause incorrect results or inefficiency.
medium
A. Line 5: Incorrect base case initialization
B. Line 1: Using list instead of set for word lookup
C. Line 8: Incorrect substring indices
D. Line 9: Setting dp[i] to True without checking all lengths

Solution

  1. Step 1: Analyze data structure for word lookup

    Using a list for word_set causes substring membership checks to be O(n) instead of O(1), leading to inefficiency and possible timeouts.
  2. Step 2: Verify other lines

    Base case dp[0] = True is correct; substring indices are correct; missing break after dp[i] = True causes inefficiency but not incorrectness.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    List lookup is linear, set lookup is constant time [OK]
Hint: Use set for O(1) membership, not list [OK]
Common Mistakes:
  • Forgetting to break inner loop
  • Misindexing substring
  • Incorrect base case initialization
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