Replace Values and Return Sums
Updated
Problem statement
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.
More Amazon Online Assessment questions
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Get Smallest Base SegmentOA · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026