← Questions
Maximum sum of non-adjacent houses, on a line, a circle and a tree
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Airbnb's coding round and at Google, Databricks and 1 more company, last asked Jan 2026. It tests dynamic programming and trees.
Each house holds some amount of money, and you may not take from two neighboring houses. Find the largest total you can take. The problem comes in three versions, each built on the last.
Part 1: houses in a row
-
rob_line(nums: list[int]) -> int, where houseiand housei + 1are neighbors.
rob_line([2, 7, 9, 3, 1]) -> 12 # 2 + 9 + 1
rob_line([]) -> 0
Part 2: houses in a circle
-
rob_circle(nums: list[int]) -> int. Now the first and last houses are also neighbors.
rob_circle([2, 3, 2]) -> 3 # 2 and 2 are neighbors on the circle
rob_circle([1, 2, 3, 1]) -> 4 # 1 + 3
rob_circle([5]) -> 5
Part 3: houses in a binary tree
-
rob_tree(root: TreeNode) -> int. A parent and its child are neighbors.
3
/ \
4 5
/ \ \
1 3 1
rob_tree(root) -> 9 # 4 + 5
3
/ \
2 3
\ \
3 1
rob_tree(root) -> 7 # 3 + 3 + 1
Constraints
- Up to 10^5 houses or tree nodes; amounts are non-negative integers up to 10^4.
- O(n) time for every part; O(1) extra memory for Parts 1 and 2.
Clarifying questions
- Can amounts be negative? (If so, taking nothing from a house is always allowed.)
- Should the answer include which houses to take?
- Part 3: can the tree be very deep, so recursion might overflow the stack?
What a strong answer covers
- Part 1: the recurrence
best[i] = max(best[i-1], best[i-2] + nums[i]), kept in two variables. - Part 2: break the circle by solving the line twice, once without the last house and once without the first, and taking the larger; handling one house separately.
- Part 3: a post-order traversal returning two values per node, the best total with the node taken and with it skipped; why memoizing a single value per node is not enough without care.
- An iterative traversal for deep trees.
- Tests: empty input, one house, two houses, all equal values, a skewed tree.
Follow-ups
- Return the set of houses taken as well as the total.
- You must leave at least two houses between any two you take.
- The houses form a general graph. Why does the problem become hard?
Where it's asked
- Airbnb · Coding · Last asked Jan 202645 min · Live coding, one or two rounds, from the same pool as the screen
- Google · First round: coding · Last asked Jan 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
- Databricks · Algorithms (two rounds) · Last asked Nov 202560 min · Live coding in CoderPad or similar
- Amazon · Technical phone screen · Last asked Oct 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding


