DSA Tracker

Medium

Design Add and Search Words Data Structure

A medium Trie problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Trie
Sheets
2
Core for
7 roles
Platform
LeetCode

The problem

Design a data structure that supports adding words and searching for words, where search queries may contain a dot '.' as a wildcard that matches any single lowercase letter.

Example 1

Input
addWord("bad"), addWord("dad"), addWord("mad"), search("pad"), search("bad"), search(".ad"), search("b..")
Output
false, true, true, true

Example 2

Input
addWord("a"), addWord("a"), search("a"), search("aa"), search("a.")
Output
true, false, true

Constraints

  • Words consist of lowercase English letters
  • '.' in search queries matches any single letter
  • At most 10^4 calls in total

How to think about it

Updated 2026-09-09

Exact characters dictate a deterministic single-step transition, while a wildcard forces the search to explore all existing outgoing edges at that step. Structuring the dictionary as a prefix tree isolates wildcard branching only to nodes where transitions actually exist, keeping search bounded by the valid dictionary branches.

Approaches, worst first

  1. Word list regex matching

    time O(n * L) · space O(total characters)

    Store all words in an array or grouped by length. Convert wildcard queries into regular expressions and test against each stored candidate. Straightforward to implement, but scans every stored word on every wildcard query.

  2. Trie with recursive branchingWrite this one

    time O(26^d * L) · space O(total characters)

    Build a trie for additions. On search, step deterministically for standard letters and branch across all non-null children whenever encountering a dot. The first branch returning true short-circuits the search.

Where people lose marks · 3
  • Failing to short-circuit upon finding a match among child branches during wildcard traversal, doing redundant work across alternative branches.
  • Assuming a dot matches empty strings: a wildcard must consume exactly one letter.
  • Returning true at the end of the query string even if the landing trie node is not marked as the terminal character of a word.

The theory behind it

Trie — the ground this problem stands on. All Trie problems

What Trie is

A trie, also called a prefix tree, is a tree structured for storing words character by character. Instead of storing entire words in individual nodes, each step down a branch represents a single letter. Words that share the same beginning, like car, card, and care, share the exact same starting path down the tree. A special boolean marker sits at the end of each valid word to show that a complete word terminates at that letter.

When to reach for it

Reach for a trie when questions involve prefix lookups, dictionary word searches, autocomplete engines, or matching prefixes against a body of text. Prompts asking whether any word in a dictionary begins with a given prefix, or searching for words on a Boggle board grid, point directly to a trie. It also applies to bitwise tasks, such as finding the maximum XOR pair among integers by treating numbers as binary prefixes.

How the pattern works

Represent each trie node with an array or hash map of child links, plus a boolean flag marking if a complete word ends at that node. When inserting, start at the root and walk down character by character, creating new child nodes whenever a path does not exist yet, then mark the final node as a word ending. When searching, follow the characters; if any child link is missing, the word or prefix does not exist. If all characters match, check the boolean flag to distinguish between a full word and a partial prefix.

What each operation costs

OperationTime
insert word of length l into trieO(l)
search for full word of length lO(l)
check if any word begins with prefix of length lO(l)
What usually goes wrong with Trie
  • Confusing prefix matches with full word matches by returning true when characters exist but the end-of-word flag on the final node was never set.
  • Allocating fixed 26-slot arrays for child pointers without verifying that all input characters are strictly lowercase English letters.
  • Forgetting to prune unvisited branches during board searches, leading to time limit exceeded errors on grids with large word sets.

Which roles need this problem

Trie is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 2 more roles, including Information Retrieval Engineer, Storage Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Trie problems

Problem set and role mapping as of .