Lexicographic Word Frequencies
Reported by candidates from Upstart's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Upstart OA, reported in July 2026, is sorting by count out of habit. This one wants the rows ordered by the word itself, and the counts must come back as strings. It's a hash-table counting problem with a sort at the end, so it looks trivial. That's the trap. Candidates rush it, return integers, or forget the empty input case. If you blank under the clock, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net. But this one is simple enough that you should be able to do it yourself.
The problem
Given an array of words words, count each distinct word and return the counts in lexicographic order. Words contain lowercase English letters and comparisons are exact. Return a two-dimensional string array where every row is [word, decimalCount]. Return an empty array when the input is empty. Function wordFrequencies(words: String[]) → String[][] Examples Example 1 words = ["pear","apple","pear","banana","apple","apple"] return = [["apple","3"],["banana","1"],["pear","2"]] apple appears three times, banana once, and pear twice. The rows are ordered by word. Example 2 words = [] return = [] An empty input has no frequency rows. Example 3 words = ["zoo","ant","zoo","bee","ant"] return = [["ant","2"],["bee","1"],["zoo","2"]] The distinct words are returned in ascending lexicographic order with decimal counts. Constraints 0 <= words.length <= 200000 Every word has length from 1 through 40. Every word contains only lowercase English letters. The total number of input characters is at most 10^6.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two steps. First, build a hash map from word to count in one pass over the array. Second, take the distinct keys, sort them lexicographically, and emit [word, String(count)] for each. With up to 200000 words, that's O(n) counting plus O(k log k) sorting over distinct words, which is fine. The pitfalls are all in the details. Return counts as decimal strings, not numbers. Return an empty array for empty input, not null. Don't sort by frequency, and don't sort the raw input array and then count if you can avoid it. Since every word is lowercase letters only, default string comparison matches lexicographic order, so you don't need a custom comparator. In Java use a TreeMap to skip the explicit sort. If you freeze on the output format during the live OA, StealthCoder is the hedge that gets you unstuck.
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 Lexicographic Word 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 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 Upstart's OA.
Upstart 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.
Lexicographic Word Frequencies FAQ
How hard is Lexicographic Word Frequencies really?+
Easy. It's a frequency count plus a sort. The difficulty is in the output format: a 2D string array with counts as decimal strings. Most failed attempts come from returning integers or ordering by count instead of by word.
What's the trick to solve it fast?+
Count with a hash map, then sort the distinct keys and build rows of [word, count as string]. Lowercase-only words mean the default string sort is already lexicographic. A TreeMap in Java or sorted keys in Python gets you there in a few lines.
What's the time complexity I should aim for?+
O(n) to count plus O(k log k) to sort the k distinct words, with comparisons costing up to 40 characters each. That fits easily within the constraints of 200000 words and 10^6 total characters. Don't sort the full input first.
What edge cases should I test?+
Empty input must return an empty array. A single word should return one row with count "1". Words that are prefixes of others, like "ant" and "ante", should order the shorter one first. Check counts are strings, not integers.
How do I prepare for this in 48 hours?+
Write it once in your OA language from memory. Practice building a map, sorting keys, and converting ints to strings. Then do two similar variants, like sorting by count with ties broken by word. That covers nearly every counting-and-sort question you could see.