Dictionary Words With a Prefix
Updated
Problem statement
Given an array of unique lowercase English words dictionary and a lowercase string prefix, return every dictionary word that starts with prefix.
Return matching words in alphabetical order. An empty prefix matches every word.
Function
wordsWithPrefix(dictionary: String[], prefix: String) → String[]Examples
Example 1
dictionary = ["apple","app","bat"]prefix = "app"return = ["app","apple"]Both app and apple begin with app, and lexicographic order places the shorter word first.
Example 2
dictionary = ["dog","cat","door"]prefix = "z"return = []No word begins with z.
Example 3
dictionary = ["beta","alpha","alpine"]prefix = ""return = ["alpha","alpine","beta"]The empty prefix matches every word, so all words are sorted.
Example 4
dictionary = ["google","good","grape"]prefix = "goo"return = ["good","google"]Both good and google start with goo, but grape does not. good comes first because its fourth letter, d, comes before g.
Constraints
0 <= dictionary.length <= 100000.1 <= dictionary[i].length <= 100.0 <= prefix.length <= 100.- All words are unique and all strings contain only lowercase English letters.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Find Unmet Users With Shared InterestsPalantir · ONSITE INTERVIEWPalantir interview questions
- Three Word Suggestions for Every PrefixThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Count Proper Prefix MatchesAT&T · OAAT&T interview questions
- Product Record SearchWells Fargo · OAWells Fargo interview questions
- Find the Words for Each PrefixDocuSign · PHONE SCREENDocuSign interview questions
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Sort Topic Names by Prefix and NumberPerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Sort Words by LengthMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Sort Sentence WordsInfosys · ONSITE INTERVIEWInfosys interview questions
- Check AnagramsDeloitte · ONSITE INTERVIEWDeloitte interview questions