Skills101
← All practice quizzes

Quiz: Tries (Prefix Trees)

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

A trie stores words letter by letter so that words with the same beginning share one path. Build one, search it, count words with a prefix and list autocomplete suggestions, in Python and Java.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    What does the end-of-word flag in a trie node tell you?

  2. Question 2

    What does this print?

    root = {}
    for w in ["to", "tea", "ten"]:
        node = root
        for ch in w:
            node = node.setdefault(ch, {})
        node["$"] = True
    
    def count_nodes(n):
        return sum(1 + count_nodes(c) for k, c in n.items() if k != "$")
    
    print(count_nodes(root))
    
  3. Question 3

    Words cat, car and cart are stored. What does search("ca") return?

  4. Question 4

    What is the time to insert a word of length L into a trie that already holds n words?

  5. Question 5

    Each node keeps a count of words passing through it. How do you answer "how many words start with p"?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →