Sort Characters by Frequency
Medium · Heap & Priority Queue · about 15 minutes
Asked at Meta, Bloomberg, Microsoft, Google, Amazon, Goldman Sachs, Hudson River Trading, Jump Trading.
Rearrange the characters of s so they're grouped by how often they occur: all copies of the most common character first, then all copies of the next most common, and so on. Break ties between equally common characters by character code, smaller first (so '1' precedes 'A', and 'A' precedes 'a').