Reported May 2020
Bloomberghash table

Sort Characters by Frequency

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this Bloomberg OA, reported in May 2020, is skipping the tie-break. Sort Characters by Frequency looks like a warm-up, so people count characters, sort by count, and submit. Then "cccaaa" comes back as "cccaaa" instead of "aaaccc" and the hidden tests fail. The rule here is stricter than the classic version: equal frequencies go to the smaller ASCII code first. It's a hash-table counting problem with a custom sort on top. If you blank on the comparator during the live assessment, StealthCoder is the invisible safety net that reads the problem and hands you the working approach.

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 is two steps. Count each character with a hash map or a 128-slot array. Then sort the distinct characters by (count descending, ASCII ascending) and build the output by repeating each character count times. The pitfall is the tie-break. A frequency-only sort leaves ties in arbitrary order, so "cccaaa" can come out wrong. Example 3 shows the other trap: "Aabb" returns "bbAa" because uppercase A (65) is less than lowercase a (97). Don't lowercase anything, and don't sort alphabetically with a case-insensitive compare. With s up to 200000 characters and only letters and digits, at most 62 distinct characters get sorted, so runtime is effectively linear. Build the result with a list or string builder, not repeated concatenation in a loop. If the comparator slips away mid-assessment, StealthCoder is the hedge that surfaces it while you keep typing.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as sort characters by frequency. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg 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 part that costs people is the ASCII tie-break, which the classic LeetCode version doesn't require. Get the comparator right and you're done in about fifteen lines. Miss it and you'll pass the samples but fail hidden tests.

What's the trick for the tie-break rule?+

Sort distinct characters with a key of (negative count, character code). Higher count comes first, and on equal counts the smaller ASCII value wins. In most languages that's a tuple key or a two-step comparator. Uppercase sorts before lowercase automatically if you compare raw codes.

Do I need a heap or bucket sort?+

No. Only letters and digits appear, so there are at most 62 distinct characters. Sorting them is cheap. A heap or bucket approach works, but it adds code and more places to slip on the tie-break. Keep it simple with a count map and a sort.

Is this hash-table pattern still asked at Bloomberg?+

This one was reported in May 2020, so treat it as a known shape rather than a guarantee. Frequency counting plus a custom sort is a common OA pattern. Being able to write a count map and a comparator quickly covers a lot of similar questions.

How do I prepare for this in 48 hours?+

Write the solution once from scratch and run all three examples, especially "Aabb" and "cccaaa". Then change the tie-break to confirm your output flips. Check that you build the string efficiently. That's enough for this problem and for its variants.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it