We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Words With Prefix

In a trie, "hello", "help" and "hi" would be represented as:

{"h": {"e": {"l": {"l": {"o": {"*": True}}, "p": {"*": True}}}, "i": {"*": True}}}

Assignment

Complete the search_level function. This recursive function collects all complete words starting from the current trie level. It takes the current dictionary level, the accumulated prefix string, and the list of words found.

    • If the current level contains an end marker, add the current prefix to the words collection.
    • Extend the prefix with the current character (e.g., current_prefix + character, rather than modifying the prefix in place).
    • Recursively search the child level with the extended prefix.

Complete the words_with_prefix function. This finds all words in the trie that begin with a given prefix.

    • If the character doesn't exist in the current level, return an empty list: no words start with this prefix.
    • If the character does exist, move to the child level corresponding to the current character.