House Robber
Updated
Problem statement
nums[i] is the amount available at position i along a linear street. Choose positions with the largest possible total amount. You cannot choose two positions next to each other.
Return the maximum total as an integer. You may select no positions, giving 0. The first and last positions are adjacent only when they are consecutive indices; the street is not circular. Return the value only, not a list of selected indices. Do not modify nums.
Function
rob(nums: int[]) → intExamples
Example 1
nums = [1,2,3,1]return = 4Choose the first and third entries: 1 + 3 = 4.
Example 2
nums = [2,7,9,3,1]return = 12Choose positions 0, 2 and 4: 2 + 9 + 1 = 12.
Example 3
nums = [2,1,1,2]return = 4Choosing the first and last entries gives 4; picking only one index parity would miss this combination.
Constraints
1 <= nums.length <= 100.0 <= nums[i] <= 400.- At most 50 positions can be selected, so the result is at most
20000and fits a signed 32-bit integer.
More Headlands Technologies interview questions
- As-Of JoinOA · ONSITE INTERVIEW · Seen Aug 2026
- Triggered Order Event FilterONSITE INTERVIEW · Seen Oct 2024
- Best Time to Buy and Sell StockPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2024
- Best Time to Buy and Sell Stock IIONSITE INTERVIEW · Seen Apr 2024
- Merge IntervalsONSITE INTERVIEW · Seen Apr 2024
- Net Position Calculation from API SnapshotsONSITE INTERVIEW · Seen Apr 2024
- Disaster RecoveryOA · Seen Nov 2026
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Array Interval SumsOA · Seen Oct 2026
- Job Runner Chain ReportsOA · Seen Oct 2026
Similar questions at other companies
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximum SubarrayUpstart · ONSITE INTERVIEWUpstart interview questions
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions