Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
DATA STRUCTURES

Tries

Prefix trees that share common beginnings between words.

Advanced6 problems2 interactive subtopicsO(L) per word
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (2)Open in full playground →
STEP 1/4Insert “cat”
insert "cat"
root
c
a
t
r
end
  • visited / done
  • active this step
  • read by this step
  • result
Space Play←→ StepR Replay
1 / 4
PYTHONinsert("cat"), insert("car"), starts_with("ca")
1class Trie:
2 def __init__(self):
3 self.root = {}
4
5 def insert(self, word):
6 node = self.root
7 for ch in word:
8 node = node.setdefault(ch, {})
▶9 node["$"] = True
10
11 def starts_with(self, prefix):
12 node = self.root
13 for ch in prefix:
14 if ch not in node:
15 return False
16 node = node[ch]
17 return True
STEP 1 OF 4Setup

Insert “cat”

Creates nodes for c, a and t, and marks t as the end of a word.

VARIABLES

word
"cat"
root branches
c
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A trie stores strings one character per level. Each node maps a character to a child node, and a flag marks where a word ends.

Why it matters

Prefix lookups take O(L) for a word of length L, however many words are stored.

Think of it like this

A phone’s autocomplete: typing “ca” narrows to every word under that branch.

Use it when

  • Prefix search and autocomplete
  • Searching many words against a board or text
  • Longest common prefix questions

Avoid it when

  • Exact lookups only - a hash set is simpler
  • Very few words - the node overhead isn’t worth it

Complexity cheatsheet

OperationTimeSpace
Insert a word of length LO(L)O(L) new nodes
Search or startsWithO(L)O(1)
Build from many wordsO(total characters)O(total characters)

Often confused with: hash sets - they answer “is this word present?” but not “which words start with this?”

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

prefixstartsWithautocompletedictionary of wordsword search
  • “Implement insert, search and startsWith” is the classic trie prompt
  • Many words in a grid: build a trie once, then run DFS
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Forgetting the end-of-word marker - “ca” would look like a word
  • Confusing search (needs the marker) with startsWith (doesn’t)
  • Creating nodes while searching

Edge cases to test

  • An empty string as a word or prefix
  • One word that is a prefix of another (“car”, “card”)
  • A prefix longer than any stored word
Interview tip: A dict-of-dicts trie is fast to write in Python; mention a 26-slot array as the memory-tight alternative.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

After inserting only “card”, what does search("car") return?

06

Practice

Apply what you just watched on real interview problems.

Generate String problem in AI DSA Practice →
  • BeginnerLongest Common PrefixLeetCode 14 ↗ (opens in a new tab)
  • IntermediateImplement Trie (Prefix Tree)LeetCode 208 ↗ (opens in a new tab)
  • AdvancedWord Search IILeetCode 212 ↗ (opens in a new tab)
GraphsTwo Pointers