Skip to main content

82. House Robber

mediumAsked at Datadog

Max-sum subsequence with no two adjacent elements. Datadog uses this as the simplest 1D DP question — same shape as their non-overlapping window aggregation over a metric stream.

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

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Datadog phone screen DP warmup.

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

Example 2

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

Approaches

1. Recursive without memo

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

Time
O(2^n)
Space
O(n)
function rob(nums) { function f(i) { if (i < 0) return 0; return Math.max(f(i-2) + nums[i], f(i-1)); } return f(nums.length - 1); }

Tradeoff: Exponential — memoize.

2. Rolling 2-state DP (optimal)

Track prev2 (i-2) and prev1 (i-1). curr = 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: O(1) state. Datadog-canonical rolling DP.

Datadog-specific tips

Datadog will follow up with House Robber II (circular) — first/last are adjacent. Solve as two independent linear House-Robber problems: rob[0..n-2] and rob[1..n-1]; take the max.

Common mistakes

  • Tracking three states (prev2, prev1, curr) without rolling — wastes memory.
  • Forgetting that all values are non-negative — simplifies the comparison.
  • Adding nums[i] to prev1 instead of prev2.

Follow-up questions

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

  • House Robber II (LC 213) — circular street.
  • House Robber III (LC 337) — tree-shaped street.
  • Datadog-style: non-overlapping window aggregation.

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 rolling DP?

Only the last two states matter. We can throw away everything older.

What if values can be negative?

Add a max(0, ...) on the chosen branch. The empty-subsequence option becomes meaningful.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →