Top K Frequent Words
Updated
Problem statement
You are given an array words and an integer k. Return the k different words that appear most often. Each array entry counts as one occurrence of the whole word.
Put words with more occurrences first. When two words have the same count, put the word that comes first alphabetically before the other. Return only the selected words, in this ranking order, without their counts.
All words contain lowercase English letters. Compare them character by character in alphabetical order; if one is a prefix of the other, the shorter word comes first. Do not combine prefixes, inflections, or repeated entries into a different token. If k is zero, return an empty array.
Function
topKWords(words: String[], k: int) → String[]Examples
Example 1
words = ["pear","apple","pear","banana","apple","pear"]k = 2return = ["pear","apple"]pear occurs three times, apple twice, and banana once, so the first two ranked words are pear and apple.
Example 2
words = ["dog","cat","dog","cat","ant"]k = 2return = ["cat","dog"]cat and dog both occur twice. Lexicographic order places cat before dog; ant occurs only once.
Example 3
words = ["book","book","page"]k = 0return = []Selecting zero words returns an empty array even when the input is nonempty.
Constraints
0 <= words.length <= 300.- Every word contains from
1through20lowercase English letters. 0 <= k <=the number of distinct words inwords.- The complete input is a finite batch. There are no updates, time windows, prices, or quantities attached to an occurrence.
More Snowflake Phone Screen questions
- Minimum StackPHONE SCREEN · Seen Nov 2026
- Design In-Memory File SystemPHONE SCREEN · Seen Nov 2026
- Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Bank Account Balances (Part 2): Monthly ReconciliationStripe · PHONE SCREENStripe interview questions
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
- Top Ten Most Frequent Words in a BookRobinhood · PHONE SCREENRobinhood interview questions
- Group AnagramsEPAM · PHONE SCREENEPAM interview questions
- Aggregate Matching IPv4 Addresses Across ResponsesPinterest · PHONE SCREENPinterest interview questions
- Diff Two Filesystem Hash TreesCursor · PHONE SCREENCursor interview questions
- Find Duplicate File GroupsAnthropic · PHONE SCREENAnthropic interview questions
- Group AnagramsJFrog · PHONE SCREEN