Coding interview problems โบ Dynamic Programming
๐ช Dynamic Programming Medium
House Robber
House Robber explained: the take-or-skip DP for the maximum sum of non-adjacent elements, O(n) time and O(1) space, tested Python and a live DP visual.
Also searched as: house robber dp ยท maximum sum of non-adjacent elements ยท max sum no two adjacent
Try it live Free
Coin Change (DP)
The Coin Change lab shows the same idea in a 1D table: watch dp[] fill left to right, each cell built from earlier answers, which is exactly how best(i) is built from best(i-1) and best(i-2).
Also useful: Recursion & the Call Stack
The problem
A row of houses each holds some amount of money (non-negative integers). You may take money from any houses you like, but never from two neighbouring houses. Return the largest total you can collect.
Take 2 + 9 + 1.
Take the first and last house.
The key insight
At each house you make one decision: take it (then you add its money to the best total from two houses back) or skip it (then you keep the best total up to the previous house). The best answer up to house i is the larger of those two, and it depends only on the previous two answers.
Step-by-step approach
- Let
best(i)be the most money from houses0..i. - Recurrence:
best(i) = max(best(i-1), best(i-2) + nums[i]). - Start with
prev2 = 0andprev1 = 0(no houses yet). - For each house, compute the new best and shift the two variables forward.
- Return
prev1after the last house.
Python solution
def rob(nums):
prev2, prev1 = 0, 0 # best up to i-2, best up to i-1
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1Complexity
One pass over the houses with constant work each, and only two running values are kept.
Common mistakes
- Greedily taking every other house (all even or all odd indices).
[2, 1, 1, 2]breaks that: the best is 4, from houses 0 and 3. - Initialising with
nums[0]andnums[1]directly, which crashes on empty or single-house inputs. - Writing
best(i) = best(i-2) + nums[i]only, forgetting the option of skipping housei.
Frequently asked questions
What is the time and space complexity of House Robber?
O(n) time with a single pass, and O(1) space because each answer only needs the previous two.
How do I solve House Robber when the houses are in a circle?
The first and last houses become neighbours. Run the linear solution twice, once on houses 0..n-2 and once on 1..n-1, and return the larger result (with a special case for a single house).
How do I recover which houses were robbed?
Keep the full dp array, then walk backwards: if dp[i] equals dp[i-1], house i was skipped; otherwise it was taken and you jump to i-2.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Dynamic Programming Coin Change (DP) lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.