Maximum Score With Non-Adjacent Values
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in April 2026, and the first thing to notice is that the only stated constraint is nums.length >= 1. No upper bound means you can't try every subset of values, and you shouldn't try. This is Delete and Earn in disguise, a dynamic programming problem hiding behind a counting step. If you've got an OA invite and 48 hours, learn this shape and you're set. It's a house-robber variation on the value line instead of the array index. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and hands you the recurrence.
The problem
You are given a list of integers nums. You may choose any set of values from the list. If you choose a value x, then you cannot choose x - 1 or x + 1. When you choose x, your score increases by x * frequency(x), where frequency(x) is the number of times x appears in nums. Return the maximum score you can obtain. Function maximumNonAdjacentValueScore(nums: int[]) → long Examples Example 1 nums = [3,4,2] return = 6 Choose values 2 and 4 for score 2 + 4 = 6. Choosing 3 would block both. Example 2 nums = [2,2,3,3,3,4] return = 9 Choosing value 3 gives score 3 * 3 = 9, which is better than choosing 2 and 4 for score 8. Constraints nums.length >= 1
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop thinking about the list and think about values. Count how many times each value appears, then the total gain for picking x is x times its count. Picking x blocks x-1 and x+1, which is exactly the house robber rule on a number line. Build an array indexed by value up to the max, or sort the distinct keys. Then run take/skip: dp[i] = max(dp[i-1], dp[i-2] + gain[i]). The pitfall is gaps. If distinct values are 2 and 5, they don't conflict, so you can't blindly apply the i-2 rule on sorted keys. Check whether the previous key is exactly x-1. Also, use a 64-bit type, since the return is long and sums of x times frequency overflow 32 bits fast. Huge values make a max-sized array wasteful, so prefer a hash map plus sorted keys. If the live OA freezes you, StealthCoder is the hedge that reads the problem and gives you the working code.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Maximum Score With Non-Adjacent Values cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as delete and earn. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Score With Non-Adjacent Values FAQ
What's the trick in Maximum Score With Non-Adjacent Values?+
Convert the list into a gain per distinct value: value times frequency. Choosing x forbids x-1 and x+1, so it becomes house robber over the sorted values. Each step is take or skip, where taking adds the gain to the best from two values back.
How hard is this really for an Amazon OA?+
Medium. The DP is short once you see it, but the reduction from a raw list to a value-indexed gain is the part people miss. If you've seen house robber, you can finish in about 15 minutes. If you haven't, expect to struggle with the setup.
Why can't I just use a plain house robber on sorted distinct keys?+
Because gaps break adjacency. If your keys are 2 and 5, both can be taken. Only when the previous key equals x-1 do you need the skip rule. When it's not adjacent, you add the gain to the full previous best.
What data types should I watch for?+
The function returns long, so use 64-bit accumulation. A value times a large frequency can overflow a 32-bit int. Also avoid allocating an array up to the max value if values can be huge. Use a hash map and sort the keys instead.
How do I prepare for this in 48 hours?+
Solve house robber first, then Delete and Earn, which is this same problem. Practice writing the counting step and the adjacency check from memory. Test with [3,4,2] and [2,2,3,3,3,4], plus a case with gaps between values.