Top Ten Most Frequent Words in a Book
Reported by candidates from Robinhood's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Robinhood reported this one in September 2026, and the first thing to notice is the output format: each entry is a single string like "red 3", not a pair. The task is to split a book into ASCII letter-or-digit words, lowercase them, count them, and return the top ten with ties broken alphabetically. It's a hash-table counting problem with a sort on top. The text can run to a million characters, so sloppy parsing will hurt. If you blank on the tie-break or the tokenizer during the live OA, StealthCoder runs invisibly as a safety net and gives you the working solution.
The problem
You are given a string text containing a book. Split it into maximal ASCII-letter-or-digit words. Matching is case-insensitive, so normalize every word to lowercase. Return at most ten entries in the form "word frequency", ordered by descending frequency. Break equal-frequency ties lexicographically by normalized word. If the book contains fewer than ten distinct words, return every distinct word. Function topTenWords(text: String) → String[] Examples Example 1 text = "Red blue red GREEN blue red" return = ["red 3","blue 2","green 1"] Case variants are merged. Only three distinct normalized words occur. Example 2 text = "k j i h g f e d c b a" return = ["a 1","b 1","c 1","d 1","e 1","f 1","g 1","h 1","i 1","j 1"] All eleven words tie, so lexical order selects the first ten and excludes k. Constraints 1 <= text.length <= 1000000 text contains printable ASCII characters and whitespace.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two clean steps. First, tokenize by scanning once and treating any character that isn't an ASCII letter or digit as a separator. Don't use a regex split on whitespace, because punctuation also breaks words. Lowercase as you build each word, and push it into a hash map of counts. Second, sort the distinct entries by count descending, then word ascending, and take the first ten. Sorting the distinct words is fine at this size. A heap of size ten also works if you want to be tidy. The common pitfalls: forgetting the final word when the text ends on a letter, using locale-aware lowercasing or isalpha on non-ASCII characters, and sorting ties by insertion order. Format each result as word, a space, then the count. StealthCoder is your hedge on the live OA if the comparator logic slips under pressure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Top Ten Most Frequent Words in a Book 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Robinhood's OA.
Robinhood reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Top Ten Most Frequent Words in a Book FAQ
How hard is the Robinhood top ten words problem really?+
Easy to medium. The algorithm is a hash map plus a sort. The difficulty is in the details: correct tokenizing, case folding, and the tie-break rule. Most failures come from an off-by-one on the last word or an unstable tie order, not from the idea itself.
What's the trick to tokenizing the text?+
Walk the string once. If a character is an ASCII letter or digit, lowercase it and append it to the current word. Otherwise, end the current word if it's non-empty. After the loop, flush any leftover word. This avoids regex surprises and runs in linear time.
How do I handle ties correctly?+
Sort with a comparator: higher count first, then the lexicographically smaller word first. Example 2 shows this, where eleven words tie at one and only k is dropped. Compare normalized lowercase words, not the original text.
Do I need a heap or is sorting enough?+
Sorting all distinct words is enough. With text up to 1000000 characters, distinct words are bounded well below that, so an O(n log n) sort passes. A size-ten heap is a valid optimization but adds comparator bugs for no real gain.
How do I prepare for this in 48 hours?+
Write the solution once from scratch. Test the three edge cases: fewer than ten distinct words, digits mixed with letters, and text ending without a separator. Practice building the output strings as word plus space plus count. That covers nearly every way this question goes wrong.