Back to Practice
#0241

Trie Word Search and Prefix

MediumDSA28 min20 XP

Problem

Given words and queries, build a trie. Each query is ["word", text] for exact search or ["prefix", text] for prefix search. Return a list of booleans.

Why This Matters

Tries are the natural structure for autocomplete, dictionary lookup, and prefix-heavy string problems.

Function Signature

def trie_queries(words, queries):

Examples

Example 1
Inputwords = ["cat", "car", "dog"], queries = [["word", "cat"], ["word", "ca"], ["prefix", "ca"]]
Output[True, False, True]

"cat" is a full word, "ca" is only a prefix, and prefix "ca" exists.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Word versus prefix
Inputwords = ["cat", "car", "dog"]
Expected[True, False, True]

A prefix is not automatically a full word.

Missing branch
Inputwords = ["app", "apple"]
Expected[True, True, False]

app is a word and prefix, but ape is missing.

Hidden Test Categories
empty word listword that is also a prefixqueries longer than any word