Reported January 2026
Amazonhash table

Replace Values and Return Sums

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

The array is [1, 2, 1, 3], the transaction is [1, 4], and the sum jumps to 13. That's the shape of the Amazon OA reported in January 2026. It looks like a simulation problem, and that's the trap. Rewrite the array on every transaction and you're doing O(n * m) work. The real answer is a frequency map plus a running sum. If you blank on the bookkeeping during the assessment, StealthCoder runs invisibly on your desktop and gives you the working solution in real time. Better to know the trick before you open the invite, though.

The problem

You are given an integer array entries and a 2D integer array transactions. Each transaction is a pair [oldValue, newValue].
For each transaction, replace every occurrence of oldValue in entries with newValue. After applying the transaction, record the sum of all values in entries.
Return an array containing the recorded sums after each transaction.
The sums may exceed the range of a 32-bit integer.

Function
replaceValuesAndReturnSums(entries: int[], transactions: int[][]) → long[]

Examples
Example 1
entries = [1, 2, 1, 3]
transactions = [[1, 4], [2, 1], [4, 2]]
return = [13, 12, 8]
After replacing 1 with 4, the array is [4,2,4,3] with sum 13. After replacing 2 with 1, the sum is 12. After replacing 4 with 2, the sum is 8.
Example 2
entries = [5, 5]
transactions = [[1, 2], [5, 1]]
return = [10, 2]
The first transaction has no matching value, so the sum stays 10. The second transaction changes both values to 1, so the sum becomes 2.

Constraints
transactions[i].length == 2
The returned sums may require 64-bit integers.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Don't touch the array. Build a hash map of value to count, and compute the initial sum as a 64-bit number. For each transaction [old, new], look up count = map[old]. If it's zero or missing, or old equals new, record the current sum and move on. Otherwise add count * (new - old) to the sum, then add count to map[new] and delete map[old]. Each transaction is O(1), so the total is O(n + m). The classic pitfalls are overflow and the old == new case. Do the multiplication in long, not int. If old equals new and you delete after adding, you wipe out the count and corrupt every later answer. Example 2 shows the no-match case: the sum stays 10. If you freeze on the merge step in the live OA, StealthCoder is the safety net that can hand you the clean version.

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 Replace Values and Return Sums 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.

Replace Values and Return Sums FAQ

What's the trick in Replace Values and Return Sums?+

Keep a frequency map of value to count and a running sum. Each transaction changes the sum by count[old] * (new - old), then moves the count from old to new. You never rewrite the array, so every transaction is constant time.

How hard is this Amazon OA question really?+

Easy to medium. The logic is short once you see the frequency map. The brute-force version works on small inputs but fails on large ones. The difficulty is spotting that you don't need to simulate the array, and handling overflow.

Why does the problem mention 32-bit integers?+

Sums can exceed the 32-bit range, so the return type is long. Accumulate the sum in a 64-bit variable from the start. Also cast before multiplying count by the difference, or the intermediate product can overflow even if the final sum fits.

What edge cases should I test?+

Test an oldValue that isn't in the array, since the sum must stay unchanged. Test oldValue equal to newValue, where you must not delete the count. Test a newValue that already exists, so counts merge. Test repeated transactions on the same value after it's been moved.

How do I prepare for this in 48 hours?+

Practice frequency-map problems where you update an aggregate incrementally instead of recomputing it. Write this one from scratch twice in your language of choice. Focus on the merge step and 64-bit arithmetic. That covers almost everything this question tests.

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