Google VO Interview Problem: Prefix Search in a List of Words

16 Views
No Comments

Given a list, e.g. ["apple", "banana", "ant", ...], with an input prefix, find all the words starting with the prefix from the above list.

Examples:

  • prefix: "a" -> apple, ant
  • prefix: "ap" -> apple
process(words: str[])
query(prefix: str) -> str[]

Call process once, and query will be called many times.

This problem asks you to preprocess a list of words once, then answer many prefix queries efficiently. A standard solution is a Trie: build the tree in `process(words)`, then in `query(prefix)` walk to the node for that prefix and collect all words below it. An alternative is to sort the words and use binary search to find the matching range. The key tradeoff is paying a one-time preprocessing cost to make repeated prefix lookups fast.

END
 0