Amazon's search team gives you every word typed into the search box today, as a list of lower-case strings. Write top_k_words(words, k) returning the k most frequent words, most frequent first. Words with the same count are ordered alphabetically. If there are fewer than k distinct words, return them all.
Examples
top_k_words(["kindle", "case", "kindle", "charger", "case", "kindle"], 2)
['kindle', 'case']
top_k_words(["echo", "dot", "echo", "dot", "alexa"], 2)
['dot', 'echo']
What this question tests
Tests counting and ranking with a tie rule, then scale. Candidates often reach for most_common, which breaks ties by first appearance rather than alphabetically, and the follow-up is almost always "now there are millions of words".
More Python interview questions