InterviewSam
Tech TracksStudySystem DesignAptitudeDaily Quiz
Log inStart free
Tech TracksStudySystem DesignAptitudeDaily Quiz
All topics

Tries

Prefix trees for autocomplete, word search and XOR problems.

4 pages, free to read

Start studying
  1. 1

    Trie basics

    Beginner

    A trie stores strings as paths of characters from a shared root, so words with the same prefix share nodes and any lookup costs O(L) for a string of length L, no matter how many words are stored.

    9 min read
  2. 2

    Prefix search & autocomplete

    Intermediate

    Walk the trie down to the node for the typed prefix, then explore only that node's subtree to list, count or rank the words that start with it.

    10 min read
  3. 3

    Word search with a trie

    Advanced

    To find many words on a letter grid at once, put the words in a trie and run one backtracking DFS per cell that follows the trie, so every path stops the moment no word can continue it.

    12 min read
  4. 4

    Bitwise trie (maximum XOR)

    Advanced

    Store numbers in a trie by their bits from most significant to least, then for each number greedily walk toward the opposite bit at every level to find the partner that gives the largest XOR, in O(bits) per query.

    11 min read