Sort Characters by Frequency
Reported by candidates from Snap's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Snap reported this one in September 2026, and it looks friendlier than it is. Sort Characters by Frequency really reduces to one thing: count each character, then sort the distinct characters by count descending and ASCII ascending. That's a hash-table problem with a sort bolted on. The twist is the tiebreak rule, which the classic version doesn't have. If your OA is in the next day or two, learn the shape now. If you blank mid-assessment, StealthCoder is the invisible overlay that reads the problem on screen and hands you a working solution as a safety net.
The problem
Given a string s, reorder its characters so characters with higher frequencies appear before characters with lower frequencies. All copies of the same character must be contiguous. When two characters have the same frequency, place the character with the smaller ASCII code first. Return the reordered string. Uppercase and lowercase letters are distinct. Function frequencySort(s: String) → String Examples Example 1 s = "tree" return = "eert" The character e appears twice. The characters r and t appear once, so ASCII order places r before t. Example 2 s = "cccaaa" return = "aaaccc" Both characters appear three times, so the tie is resolved by ASCII order. Example 3 s = "Aabb" return = "bbAa" The two lowercase b characters come first. Among the remaining singletons, uppercase A precedes lowercase a by ASCII code. Constraints 1 <= s.length <= 200000. s contains only uppercase English letters, lowercase English letters, and digits.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: build a frequency map, then sort only the distinct characters, not the whole string. There are at most 62 distinct characters (letters and digits), so the sort is basically free. Use a comparator: higher count first, then smaller ASCII code first. Then build the output by repeating each character count times and joining. With a length up to 200000, don't do string concatenation in a loop in languages where that's quadratic. Use a list or builder. The common pitfall is forgetting the tiebreak and returning "eert" as "eetr", or lowercasing everything when case matters. Example 3, "Aabb" to "bbAa", catches that. A fixed array of 128 counts works as well as a hash map. If you freeze on the comparator during the live OA, StealthCoder is the hedge that gets you a clean version fast.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Sort Characters 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
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 Snap's OA.
Snap reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sort Characters by Frequency FAQ
How hard is Sort Characters by Frequency really?+
Easy to medium. The counting is trivial. The only real work is the two-key sort, frequency descending then ASCII ascending. Most people who miss it forget the tiebreak that Snap's version requires. Walk through the three examples and you'll catch it.
What's the trick to solving it fast?+
Count characters into a map or a 128-slot array, collect the distinct characters, sort them with a comparator on count then ASCII code, and emit each character repeated count times. Sorting at most 62 symbols keeps it simple and fast.
What's the time complexity I should state?+
O(n) to count and build the output, plus a sort over at most 62 distinct characters, which is effectively constant. Total is O(n) time and O(n) space for the result string. Say that out loud if the assessment asks for analysis.
Does case sensitivity matter here?+
Yes. Uppercase and lowercase are distinct characters, so 'A' and 'a' are counted separately. Uppercase has the smaller ASCII code, so it wins ties. Example 3, 'Aabb' becoming 'bbAa', shows exactly this.
How do I prepare for this in 48 hours?+
Write it once from scratch in your OA language, including the custom comparator. Test with 'tree', 'cccaaa', and 'Aabb'. Then check you can build a big output string efficiently for 200000 characters. That's about 20 minutes of work.