Maximum Points by Deleting Elements
Reported by candidates from Walmart's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Walmart reported this one in September 2026, and the whole solution hinges on one data structure: a frequency table indexed by value. If you reach for sorting and picking elements one at a time, you'll burn your time. This is Delete and Earn in a Walmart costume. Choose a value x, earn x for every copy of it, and neighbors x-1 and x+1 vanish. Once you see that, it's a house-robber DP over values. If your mind goes blank mid-assessment, StealthCoder runs invisibly on your desktop and can hand you the recurrence in real time.
The problem
You are given an integer array elements. Repeatedly choose one remaining element with value x and earn x points. The chosen element is removed, and every remaining element equal to x - 1 or x + 1 is deleted. Continue until no elements remain. Return the maximum total number of points you can earn. Function maxPoints(elements: int[]) → long Examples Example 1 elements = [3,4,2] return = 6 Choose 4 to earn 4 points, which deletes the neighboring value 3. Then choose 2 to earn 2 more points. The total is 4 + 2 = 6. Constraints 1 <= elements.length <= 10^5. 1 <= elements[i] <= 10^5 for every valid index i.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that picking one x means you may as well take every copy of x. Deleting neighbors doesn't hurt you, because copies of x are never deleted by choosing x. So build an array sums where sums[v] = v * count(v), sized to the max value, up to 10^5. Then it's adjacent-exclusion DP: dp[v] = max(dp[v-1], dp[v-2] + sums[v]). Two rolling variables are enough. The pitfalls are real. Totals reach about 10^5 * 10^5, so use a 64-bit type, which is why the signature returns long. Also don't treat the array as sorted by index, and don't forget values with zero count, which still sit in the chain as zeros. Time is O(n + maxValue). If you blank during the live OA, StealthCoder is the safety net that shows the frequency-then-DP structure while you type.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Maximum Points by Deleting Elements 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
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 Walmart's OA.
Walmart reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Points by Deleting Elements FAQ
What's the trick in Maximum Points by Deleting Elements?+
Group equal values. Taking one x means you take all copies of x, since they're never deleted by choosing x. Then you can't take both x and x+1, so it becomes a house-robber style DP over the value line using value times count as the weight.
How hard is this Walmart OA question really?+
Medium. The DP is short, but the leap from a deletion rule to a frequency table plus adjacent-exclusion is where people stall. Once you spot it, the code is under 15 lines. Most failures come from overflow or off-by-one on the value range.
Do I need a 64-bit integer?+
Yes. With 10^5 elements all equal to 10^5, the sum hits 10^10, which overflows a 32-bit int. The function returns long for that reason. Use long in Java or C++, and Python handles it automatically.
Can I solve it by sorting the array?+
You can, by sorting and compressing into distinct values with their totals, then running the same DP and handling gaps where a value differs by more than 1. A counting array indexed by value is simpler since values are capped at 10^5.
How do I prepare in 48 hours?+
Write the counting array and the two-variable DP from scratch a couple of times, then test with [3,4,2], all-equal arrays, and widely spaced values. Also practice a house-robber variant so the recurrence feels automatic. Check the overflow case last.