House Robber
Python data engineering interview problem. Difficulty: advanced. Pattern: Dynamic Programming. About 18 minutes. Part of the Pro drill bank.
Max money robbing non-adjacent houses. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A route of houses has money, but neighbouring houses cannot both be visited without triggering an alarm. Return the largest sum of money from a set of non-adjacent positions. You may skip houses, but you may not choose two neighbours. Write rob(nums: list) -> int. In [1,2,3,1], choose 1 and 3 for a total of 4. The starter file already contains the function name, harness and print lines. Keep them and fill only the function body.
Requirements
- Never select adjacent positions.
- Choose the maximum possible total.
- Return 0 for no houses.
Constraints
- Amounts are non-negative integers.
- The return type is int and the input order represents adjacency.
Examples
Input: rob([1, 2, 3, 1]) Output: 4 Rob house 1 and 3.
Topics: lakebench, python, dp, house robber.
More Python interview questions · All interview problems · Learn data engineering
House Robber
Interview-style drill: Max money robbing non-adjacent houses.
Implement `rob(nums: list) -> int`. Example: `[1, 2, 3, 1]` → `4` (1+3). Keep the harness.