Skip to main content

27. House Robber

mediumAsked at Reddit

Maximize sum from non-adjacent array elements. Reddit asks this to test DP intuition — the same pattern used in their abuse system to select non-adjacent (non-similar) accounts when applying rate-limiting waves.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen, DP warm-up before harder ranking-window questions.

Problem

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night. 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.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 400

Examples

Example 1

Input
nums = [1,2,3,1]
Output
4

Explanation: Rob house 1 and house 3.

Example 2

Input
nums = [2,7,9,3,1]
Output
12

Explanation: Rob 1, 3, 5.

Approaches

1. Recursion with memoization

rob(i) = max(nums[i] + rob(i+2), rob(i+1)).

Time
O(n)
Space
O(n)
function rob(nums) {
  const memo = new Map();
  function dp(i) {
    if (i >= nums.length) return 0;
    if (memo.has(i)) return memo.get(i);
    const result = Math.max(nums[i] + dp(i + 2), dp(i + 1));
    memo.set(i, result);
    return result;
  }
  return dp(0);
}

Tradeoff: Linear time and space. Recursive form clarifies the recurrence.

2. Iterative DP with O(1) space (optimal)

Roll two variables: prev2 = best up to i-2, prev1 = best up to i-1. New best = max(prev2 + nums[i], prev1).

Time
O(n)
Space
O(1)
function rob(nums) {
  let prev2 = 0, prev1 = 0;
  for (const n of nums) {
    const curr = Math.max(prev2 + n, prev1);
    prev2 = prev1;
    prev1 = curr;
  }
  return prev1;
}

Tradeoff: Constant space. The classic 'rolling window' DP.

Reddit-specific tips

Reddit interviewers want the rolling DP. Bonus signal: state the recurrence verbally before coding ('at each house we pick rob-or-skip; rob means we must skip the previous, skip means we take the previous best'). Same shape as their non-adjacent karma-decay computation.

Common mistakes

  • Initializing prev1 or prev2 incorrectly (use 0 to make i=0 work).
  • Updating prev1 before prev2 (must save curr first).
  • Trying to track which houses were chosen (problem only asks for the total).

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • House Robber II (LC 213) — circular street.
  • House Robber III (LC 337) — binary tree.
  • Return which houses were robbed.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is this O(1) space?

Each cell depends only on the previous two cells. Two scalars suffice.

What about a 'rob-or-skip' boolean DP?

Equivalent but uses 2x space. The flattened form is the same.

Practice these live with InterviewChamp.AI

Drill House Robber and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →