FastPrepAutocomplete Words by Prefix

Autocomplete Words by Prefix

Microsoft logoMicrosoft● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a vocabulary array words and a string prefix, return every distinct vocabulary word that starts with prefix.

Matching is case-sensitive. Return the results in lexicographic order.

Function

autocomplete(words: String[], prefix: String) → String[]

Examples

Example 1

words = ["dog","door","deer","doom","door"]prefix = "do"return = ["dog","doom","door"]

The three distinct matching words are returned once in lexicographic order.

Example 2

words = ["apple","banana"]prefix = "cat"return = []

No vocabulary word begins with cat.

Constraints

  • 0 <= words.length <= 100000
  • 0 <= words[i].length, prefix.length <= 100
  • Every word and the prefix contain only lowercase English letters.

More Microsoft problems

See Microsoft hiring insights
public String[] autocomplete(String[] words, String prefix) {
  // Write your code here.
}
words["dog","door","deer","doom","door"]
prefix"do"
expected["dog", "doom", "door"]
Checking account…