Reported April 2024
Amazongreedy

Max Negation

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon asked this in April 2024, and it's a numbers game that looks simpler than it is. You get a list of integers, and the goal is to maximize the result by strategically negating values. The trick isn't brute force. It's recognizing that you can flip the sign of any element once, and you want to end up with the largest possible sum. This is a greedy problem wrapped in a math optimization layer. StealthCoder reads the exact constraints and spits the pattern back to you if your brain blanks under pressure.

Pattern and pitfall

The core insight: negate the smallest absolute value if the sum is negative, otherwise leave all negatives positive. You iterate through the array, flip the sign of the element with minimum absolute value, then check if the result improved. If it didn't, undo it. The gotcha is handling the case where all numbers are negative. You must negate the largest negative (smallest absolute value) to minimize the damage. Another pitfall: not considering whether a negation actually helps. This is a greedy algorithm that requires a single pass to track min absolute value and then conditional flipping. StealthCoder will flag edge cases like all negatives, single element, and zero in the list in real time.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Max Negation 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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon 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.

Max Negation FAQ

What's the actual trick here?+

You can negate one element. Negate the smallest absolute value if your current sum is negative, otherwise leave it. If all numbers are negative, negate the one with the smallest absolute value to lose the least. The goal is always the largest sum possible.

Is this a dynamic programming problem?+

No. It's pure greedy. You scan once, find the min absolute value, conditionally flip it, and you're done. DP is overkill and will time out or waste space. Amazon wants the linear greedy solution.

What happens if the array is all negative?+

You still negate exactly one element. Pick the one closest to zero, because flipping it loses the least value. The result will still be negative, but it's the best you can do.

Can I negate multiple elements?+

The problem likely says exactly one. Re-read the constraints. If you can negate more than one, it becomes trivial: flip every negative. Amazon's version constrains you to one flip.

How do I prepare for this in 48 hours?+

Understand greedy vs DP first. Practice identifying when greedy is optimal. Run through examples: all positive, mixed, all negative. Trace the algorithm by hand. Time complexity is O(n), space is O(1). You don't need flashcards, just clarity on the one-flip constraint.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it