DSA interview question
A text-analytics widget displays a word with its most frequent characters first: "tree" becomes "eert", because 'e' appears twice and the other letters once. Every copy of a character stays together. The natural approach counts each character, then orders characters by count, most frequent first. For a deterministic display, characters with equal counts appear in increasing character order. A bucket sort by frequency brings it down to linear time.
Example: "tree" → "eert"; "cccaaa" → "aaaccc".
string frequencySort(const string& s); // higher frequency first; ties by character code, ascending
Input s | Output | Explanation |
|---|---|---|
"tree" | "eert" | e×2, then r and t (r < t) |
"cccaaa" | "aaaccc" | Tie at 3: 'a' comes before 'c' |
"Aabb" | "bbAa" | b×2, then 'A' (65) before 'a' (97) |
"z" | "z" | |
"" | "" |
(Confirm: characters are ASCII; case-sensitive; LeetCode accepts any order among ties, while this version fixes it for determinism.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in