Sort Error Codes by Frequency
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's March 2026 OA is testing frequency-based sorting with a twist. You're given error codes and need to sort by frequency first, then by value. This is a hash-table problem disguised as a sorting problem. The catch: low frequency comes first, then you break ties by numeric order. It's not hard, but the secondary sort rule trips people up. StealthCoder can feed you the pattern instantly if you blank on the tiebreaker logic.
The problem
You are given an integer array codes representing error codes. Sort the entire array using these priority rules: Return the sorted array, keeping duplicate values in the result. Function Description Complete the function sortErrorCodesByFrequency in the editor below. sortErrorCodesByFrequency has the following parameter: Returns int[]: the reordered array. 3 and 6 appear once, so they come first in numeric order. Then 4 and 5 each appear twice, so 4 comes before 5. 3 has frequency 1, 2 has frequency 2, and 1 has frequency 3.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a frequency map, then sort by two criteria: frequency ascending, then value ascending for ties. The algorithm is straightforward: count occurrences in a hash table, extract key-value pairs, apply a custom comparator, and reconstruct the output array with duplicates intact. The common mistake is reversing the frequency sort or forgetting that ties resolve by numeric value, not insertion order. Most candidates nail the hash-table part but fumble the comparator. StealthCoder acts as your safety net if you freeze on the sort logic during the live assessment.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Sort Error Codes by Frequency 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as sort characters by frequency. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sort Error Codes by Frequency FAQ
What does 'keeping duplicate values in the result' actually mean?+
It means if code 4 appears twice, your output array has two 4s. You're not deduplicating. You're reordering the input, not shrinking it. The frequency map tells you the order; the output preserves all original elements in that sorted order.
Do I sort by frequency first or by value first?+
Frequency first. Codes with lower frequency rank higher. Within the same frequency, sort by numeric value ascending. So frequency 1, then frequency 2, etc. Within each frequency group, smaller numbers come first.
Is this really just counting and custom sorting?+
Yes. Count frequencies in a hash table, extract the unique codes, sort them using a comparator with two rules, then reconstruct the output by iterating sorted codes and adding each the number of times it appeared. No dynamic programming, no greedy tricks.
What language would Amazon expect this in?+
The assessment doesn't specify, but Java, Python, and C++ are standard. Python's sort with a custom key is cleanest. Java needs a comparator. C++ uses std::sort with a lambda. The pattern is identical; the syntax differs.
How do I prepare for this in 24 hours?+
Understand the two-level sort rule. Write out the hash-table and comparator logic on paper once. Then code it once in your chosen language. Don't drill variants. You're solid if you can explain the tiebreaker rule clearly.