Editor

Trie for Autocomplete

Advanced · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Write a class Trie with insert(word), search(word) (True only for whole words that were inserted) and starts_with(prefix), which returns a sorted list of all inserted words that begin with prefix.

class Trie:
def __init__(self):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
t = Trie() for w in ["car", "cart", "care", "dog"]: t.insert(w) print(t.search("car"), t.search("ca"), t.starts_with("car"))True False ['car', 'care', 'cart']

More practice problems

All Python exercises · 50 practice problems