House Robber
Updated
Problem statement
You are planning to rob houses along a street. The houses stand in a row, and nums[i] is the amount of money in house i.
Neighboring houses share an alarm system: if two houses that are next to each other are both robbed, the alarm goes off. So you may rob any set of houses as long as no two of them are adjacent. You may skip as many houses in a row as you like.
Return the largest total amount of money you can rob without setting off the alarm.
Function
rob(nums: int[]) → intExamples
Example 1
nums = [4,1,2,7,5,3,1]return = 14Rob houses 0, 3 and 5 for 4 + 7 + 3 = 14. No two of them are next to each other.
Taking every other house does worse: houses 0, 2, 4 and 6 give 4 + 2 + 5 + 1 = 12, and houses 1, 3 and 5 give 1 + 7 + 3 = 11.
Example 2
nums = [2,9,3]return = 9Houses 0 and 2 are not neighbors and give 2 + 3 = 5. Robbing house 1 alone gives 9, which is more. House 1 is next to both other houses, so nothing can be added to it.
Example 3
nums = [6,0,0,6]return = 12Rob the first and the last house for 6 + 6 = 12. The two houses in between are skipped together, which is allowed: you may skip more than one house in a row.
Constraints
1 <= nums.length <= 10^50 <= nums[i] <= 10^4
More Microsoft Online Assessment questions
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
- Minimum Cost K-Capable ModelsOA · Seen Nov 2026
- Find Available Meeting RoomsOA · Seen Oct 2026
- Maximum Sum of a K-StarOA · Seen Oct 2026
- Capacity to Ship Packages Within DaysOA · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Subarray SumStraive · OA
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
- Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions
- API Bandwidth AllocationArista Networks · OAArista Networks interview questions
- Maximum Points by Deleting ElementsIBM · OAIBM interview questions
- House RobberWalmart · OAWalmart interview questions
- Three-Resource 0/1 KnapsackMorgan Stanley · OAMorgan Stanley interview questions