← Amazon Interview Insights

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

Intermediate
May 2026

Summary

Amazon OA for a SWE role, just one coding problem about sorting error codes by frequency. Pretty straightforward on the surface but easy to get the tie-breaking logic wrong if you rush.

Questions Asked (1)

Q1

Given an array of integer error codes, sort them so that less frequent codes appear first. If two codes have the same frequency, the smaller numeric value comes first. Return the full sorted array including duplicates.

Algorithms & Data Structures
Author's notes

I went straight for a frequency map and then sorted by (freq, value).

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, count the frequency of each error code using a hash map. Then, sort the array using a custom comparator that orders by frequency ascending, and for ties, by numeric value ascending. Finally, return the sorted array.

Pro tip: Clarify whether the input array can be modified in place or if a new array should be returned, and discuss the trade-offs between sorting the original array versus creating a new one.

1. Understand the problem

Restate the requirements: sort error codes by frequency (least frequent first), and for equal frequency, by numeric value (smallest first). Confirm that duplicates are included in the output.

2. Count frequencies

Iterate through the array and build a frequency map (e.g., using a hash map) where keys are error codes and values are their counts.

3. Define sorting criteria

Create a custom comparator that first compares frequencies, and if equal, compares the error codes numerically.

4. Sort the array

Sort the original array (or a copy) using the comparator. In languages like Java, you can sort an array of Integer objects with a custom comparator.

5. Return the result

Return the sorted array. If the original array was modified, ensure that is acceptable; otherwise, return a new sorted array.

Key Points to Mention

  • Time complexity: O(n log n) due to sorting, where n is the number of elements.
  • Space complexity: O(n) for the frequency map and possibly for the sorted output.
  • Use of a hash map for efficient frequency counting.
  • Custom comparator logic for tie-breaking.
  • Edge cases: empty array, all elements same, all frequencies distinct.
  • Stability of sorting algorithm is not required because the comparator fully defines the order.

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