Coding

Sort a String by Frequency

Sort a string by the frequency of its characters.

Input: s = "cccaaa"

Output: "cccaaa" or "aaaccc"

Explanation: Both 'c' and 'a' appear three times, so sorting them by descending frequency keeps them grouped equally.

Was asked at

Practice this question with AI

First session is free - no credit card required.

Go Premium

More interviews, more skills, more success.

No answers yet

Be the first to share your approach to this question

Practice More Questions

1 answer published. Last candidate submission on Aug 30 2026, 6:34pm PDT. Interview question asked to Software Engineers interviewing at 3M, Clubhouse, Oracle and other companies. Original question asked: Sort a String by Frequency. Question last reported by a candidate interviewing at IHS Markit for a Software Engineer interview on Oct 5 2026, 10:50am PDT.