Store words letter by letter in a trie
A trie is a tree of nested dictionaries, one letter per level. Words that share a prefix share the path for it, so finding every word that starts with "ca" means walking two steps and collecting what lies below.
root = {}
node = root
for letter in "cat":
node = node.setdefault(letter, {})
node["$"] = True # a word ends hereThink before coding
The end marker distinguishes a stored word from a mere prefix: "car" can be a word while "ca" is not. Collect words with a depth-first walk that carries the letters so far, then sort the result for a stable order. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write a Trie class with add(word), contains(word) and complete(prefix). complete returns every stored word starting with prefix, sorted.
The explanation opens here once every check passes.