Practice: Implement Trie (Prefix Tree)

Problem: https://leetcode.com/problems/implement-trie-prefix-tree/

Recognition reminder: this is a design problem, not a single function. You build a class that stores words and answers two kinds of question: is this exact word present (search), and does any stored word start with this prefix (startsWith). The lesson is that a trie node is a children map plus an end-of-word marker, and that insert, exact search, and prefix search are all the same character-by-character walk down the tree, differing only in what they do at the last step.

Before you start (the five-beat rhythm)

  1. Your Pattern Cards for this week are already written (tries and intervals).
  2. Name the pattern aloud and write your approach as a plain-English comment before any code.
  3. Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
  4. If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
  5. Debrief in your commit message before moving to the next problem.

Your target

Fill these in yourself before you look at anyone else’s solution:

Target time complexity per operation (in terms of word/prefix length):  ____
Target space complexity:                                                ____

Each operation walks the characters of the word or prefix it is given, so the time should be in terms of that length, not the number of words stored. The interesting question is what each node has to hold so that search can tell a real stored word from a prefix of a longer one, and how startsWith differs from search at the final character.

The interface you implement

Write a class Trie in a file named solution.py in this folder (your own work repo, not the course repo). It supports:

Trie()                          # initialize an empty trie
insert(word: str)               # add word to the trie (no return value)
search(word: str) -> bool       # True if the exact word was inserted before
startsWith(prefix: str) -> bool # True if any inserted word starts with prefix

The method names are exactly insert, search, and startsWith (matching LeetCode). All inputs are lowercase English letters, exactly as the LeetCode problem guarantees.

Where your code goes

This folder ships only the problem spec and a provided-example test (tests/test_provided.py). Because this is a design problem, the test does not call one function; it drives your Trie through the operation sequence from the LeetCode example and checks each output. Run it locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.

Debrief (paste into your commit message)

1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?