Skip to lesson content
navan✳THE BUILD LABDSA
0/66 complete
BUILD LAB / DSA/ Trees and hierarchy
EXPERIMENT 35 / 66

Share the prefix, not the whole word.

Represent a vocabulary by shared prefixes and distinguish a word from a path.

BY NAVAN SAMI◷ 9 MIN READCORESTRUCTURE

01 Model the idea

A trie stores keys along character paths. Common prefixes share nodes, making prefix questions natural. A terminal marker distinguishes a complete word from a prefix that merely leads to longer words. The lookup cost depends primarily on key length, while storage depends on the amount of shared prefix structure and the representation used for child maps.

THE THING THAT MUST STAY TRUE

A complete word exists exactly when its path ends at a terminal node.

02 State the strategy

Read the steps independently of syntax. Explain what each step preserves before translating it into code.

the_strategy.txt
FOLLOW or create one child per character
MARK the final node as a complete key
FOR membership, require that terminal marker

03 Make it concrete

The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.

tries.py
class Trie:
    def __init__(self): self.root = {}
    def insert(self, word):
        node = self.root
        for character in word: node = node.setdefault(character, {})
        node[None] = True
    def contains(self, word):
        node = self.root
        for character in word:
            if character not in node: return False
            node = node[character]
        return None in node
trie = Trie()
trie.insert("code"); trie.insert("coder")
print(trie.contains("code"), trie.contains("cod"))
↳ EXPECTED OUTPUT
True False
  1. 1

    The example reserves None as a terminal key; actual characters are string keys, so the marker cannot collide with a character.

  2. 2

    A path for cod exists, but its final node is not marked terminal. Exact-word lookup and prefix existence are different operations.

  3. 3

    Use an explicit normalisation policy for case, accents, and Unicode before inserting and searching. A production autocomplete system also needs ranking, result limits, and memory-aware storage.

04 Count the cost

TIME

Expected O(L) for a key of L characters

SPACE

O(total stored prefix nodes)

Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.

05 Your next move

A SMALL MISSION

Implement a prefix-existence check and distinguish it from exact membership.

Reveal one possible solution +
one_possible_solution.py
root = {}
for word in ["code", "coder"]:
    node = root
    for ch in word: node = node.setdefault(ch, {})
    node[None] = True
node = root
for ch in "cod": node = node[ch]
print(bool(node), None in node)

06 Check your reasoning

QUICK SIGNAL CHECK

Why store a terminal marker?

ANOTHER PATTERN IN YOUR TOOLKIT.

Understand it. Then own it.

Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.

Built with curiosity. By Navan Sami. ↗Patterns over panic. One step at a time.