FastPrepDictionary Words With a Prefix
Problems/Dictionary Words With a Prefix

Dictionary Words With a Prefix

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: Two Google reports describe the same task: list the dictionary words that begin with a given prefix. One of them also brings up the empty-prefix case, and its example used capitalised words; Example 4 is that example in lowercase, with the matches sorted. Neither report gives limits, a character set or an output order. Here, words are unique lowercase strings, matches come back in alphabetical order, an empty prefix matches every word, and the limits are practice choices. The judged core matches the reports at about 92%.

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 problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String[] wordsWithPrefix(String[] dictionary, String prefix) {
        // write your code here
    }
}
Checking account…