← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE coding question, probably an OA. The problem felt like it should've been a duplicate of something but I couldn't place it.

Questions Asked (1)

Q1

Given an integer array of data points and a list of queries where each query replaces all occurrences of an old value with a new value, return an array of the total sum of data points after each query is applied.

Algorithms & Data Structures
Author's notes

Spent a bit too long second-guessing edge cases.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Maintain a frequency map of values and the current total sum. For each query (old, new), if old == new, skip; otherwise, update the sum by adding (new - old) * frequency[old], then update the frequency map by moving the count from old to new. This yields O(n + q) time.

Pro tip: Clarify edge cases upfront: if old equals new, no change; if old not present, skip. Also mention that using a hash map ensures O(1) per query, which is optimal for large inputs.

1. Understand the problem

Restate the problem: given an array and queries that replace all occurrences of old with new, return the sum after each query. Confirm that queries are applied sequentially and that replacements affect subsequent queries.

2. Choose data structures

Use a hash map to store the frequency of each value in the array. Maintain a running total sum of all elements. This allows O(1) updates per query.

3. Process each query

For each query (old, new): if old == new, skip. If old not in map, skip. Otherwise, let count = freq[old]. Update sum += (new - old) * count. Then update freq[new] += count and remove old from map.

4. Collect results

After processing each query, append the current sum to the result list. Return the list after all queries.

5. Analyze complexity

Time: O(n + q) where n is array length and q is number of queries. Space: O(n) for the frequency map. This is optimal.

Key Points to Mention

  • Use a frequency map (hash map) to track counts of each value.
  • Maintain a running total sum to avoid recomputing from scratch.
  • Update sum using (new - old) * count for efficiency.
  • Handle edge cases: old == new, old not present, new already present.
  • Time complexity O(n + q) and space O(n).
  • Queries are applied sequentially, so updates affect subsequent queries.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.