Sorted Character Frequencies
Reported by candidates from Deloitte's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this Deloitte OA, reported in February 2019, is example three: twelve a's turn into "a12", not "a1" and "a2". Frequencies can run past one digit, and that's where sloppy string building breaks. The task is simple on paper. Count each lowercase letter, sort the distinct ones alphabetically, and glue each letter to its count. It's a string and counting problem with a 100000 length cap, so nothing fancy is required. If you blank on the details mid-assessment, StealthCoder runs invisibly as a safety net and hands you the solution while you keep your cool.
The problem
You are given a lowercase English word word. Count how many times each distinct character occurs. Return one string formed by listing the distinct characters in ascending alphabetical order, immediately followed by each character's decimal frequency. Function sortedCharacterFrequencies(word: String) → String Examples Example 1 word = "hello" return = "e1h1l2o1" The distinct letters are e, h, l, and o in alphabetical order, with frequencies 1, 1, 2, and 1. Example 2 word = "zzzaabb" return = "a2b2z3" Alphabetical ordering places a and b before z. Example 3 word = "aaaaaaaaaaaa" return = "a12" A frequency may contain more than one decimal digit. Constraints 1 <= word.length <= 100000 word contains only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there's nothing tricky. Make an array of 26 counters, loop through the word once, and increment counter[c - 'a']. Then walk indices 0 to 25 in order. Whenever a count is above zero, append the letter and the count as a decimal string. Walking the fixed alphabet gives you sorted order for free, so you skip a sort entirely. That's O(n) time and O(1) extra space. The common pitfalls are appending a single char instead of the full number, which breaks on counts like 12, and building the result with repeated string concatenation in a language where that's slow. Use a StringBuilder or a list join. Also don't output letters with zero count. If the live OA has you freezing on edge cases, StealthCoder is the hedge, but this one is mostly about careful output formatting.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Sorted Character Frequencies 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Deloitte's OA.
Deloitte reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sorted Character Frequencies FAQ
How hard is Sorted Character Frequencies really?+
Easy. It's a frequency count plus formatted output. The only real risk is sloppy handling of multi-digit counts or including letters that never appeared. If you can write a counting loop, you can finish this in a few minutes.
What's the trick to this problem?+
Use a 26-slot count array indexed by letter minus 'a'. Iterate it from 0 to 25 and the letters come out already sorted. No sort call, no hash map ordering issues. Append the letter, then the count converted to a string.
Do I need a hash map or sorting?+
No. A fixed array of 26 integers is simpler and faster, and iterating it in index order gives alphabetical output. A hash map works but then you'd have to sort the keys, which adds code and room for mistakes.
What edge cases should I test?+
Test a single character like "a", which should return "a1". Test a long run like twelve a's to confirm "a12". Test a word using all 26 letters, and one with repeated letters out of order, like "zzzaabb". Those cover the formatting and ordering.
How do I prepare for this in 48 hours?+
Write it once from scratch in your OA language. Practice converting an integer to a string and appending efficiently. Then do two or three similar counting problems like anagram checks. This pattern is basic, so speed and clean output matter more than depth.