Skip to main content
mediumBlind 75

Implement Trie (Prefix Tree)

Implement a trie (prefix tree) for efficient string storage and retrieval. Think about: How to represent the tree structure? Hint: Each node should track its children and whether it marks a word end. Example: insert("apple") search("apple") → True search("app") → False starts_with("app") → True

Implement Trie (Prefix Tree) is a tree problem that rewards clean recursion and a crisp definition of what each call returns. Interviewers love it because it reveals your recursive thinking and your edge-case discipline. Once your recursion contract is clear, the implementation becomes almost mechanical.

Examples

You write

Trie: insert(word: str), search(word: str), starts_with(prefix: str)

  1. Example 1

    Input
    Trie(), insert("apple"), search("apple") => true, search("app") => false, startsWith("app") => true, insert("app"), search("app") => true
    returns
    [true, false, true, true]
  2. Example 2

    Input
    Trie(), insert("hello"), search("hello") => true, search("hell") => false, startsWith("hell") => true
    returns
    [true, false, true]

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Implement Trie (Prefix Tree)medium

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.