You are given an integer array codes representing error codes.
Sort the entire array using these priority rules:
- Error codes with lower frequency come first.
- If two error codes have the same frequency, the smaller numeric value comes first.
Return the sorted array, keeping duplicate values in the result.
Complete the function sortErrorCodesByFrequency in the editor below.
sortErrorCodesByFrequency has the following parameter:
int[] codes: the input error codes
Returns
int[]: the reordered array.
Examples
01 · Example 1
codes = [4, 5, 6, 5, 4, 3] return = [3, 6, 4, 4, 5, 5]
3 and 6 appear once, so they come first in numeric order. Then 4 and 5 each appear twice, so 4 comes before 5.
02 · Example 2
codes = [2, 2, 1, 1, 1, 3] return = [3, 2, 2, 1, 1, 1]
3 has frequency 1, 2 has frequency 2, and 1 has frequency 3.
Constraints
- The output must contain the same multiset of values as the input.
- Order by increasing frequency, then by increasing numeric value.
More Amazon problems
- Closest Version DateONSITE INTERVIEW · Seen Jul 2026
- Maximum Concurrent Processes (Bar Raiser Round)ONSITE INTERVIEW · Seen Jul 2026
- Maximum Product New RatingOA · Seen Jul 2026
- Permutation SorterOA · Seen Jul 2026
- Get Distinct Pairs (Also apply to AS intern)Seen Jul 2026
- Maximum Final ValueSeen Jul 2026
- Minimum Delivery Center InconvenienceOA · Seen Jun 2026
- Unfulfilled Customers by Inventory PriorityOA · Seen Jun 2026
public int[] sortErrorCodesByFrequency(int[] codes) {
// write your code here
}
codes[4, 5, 6, 5, 4, 3]
expected[3, 6, 4, 4, 5, 5]
checking account