Interview concept

What is a trie (prefix tree)?

A trie, or prefix tree, stores strings along paths of characters so that words sharing a prefix share nodes. It supports prefix search, autocomplete, and dictionary lookups in time proportional to word length.

Interview concept

How it works

Structure

Each node represents a character, a path from the root spells a prefix, and a flag marks a complete word.

Complexity

Insert and search take O(L) for a word of length L, independent of how many words are stored.

When to use it

Reach for a trie on autocomplete, prefix matching, and word-search problems.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

When should I use a trie over a hash map?

Use a trie when you need prefix queries or ordered traversal; a hash map is better for exact-match lookups only.

What is the space cost of a trie?

Tries can use significant memory because of per-character nodes, though a compressed radix tree reduces it.