Tries Questions with Answers

30 practice questions on Tries (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 30 free in the AptiNest app.

Sample questions

Question 1Easy

Which application is NOT a typical use of a trie?

  1. Finding shortest paths in a road network
  2. Autocomplete
  3. Spell checking
  4. Longest prefix match in routers

Answer: A. Finding shortest paths in a road network

Explanation

Shortest paths need graph algorithms such as Dijkstra; the others are prefix-based lookups.

Step-by-step solution

  1. Autocomplete, spell check and longest prefix match all depend on prefixes.
  2. Shortest paths in a road network need weighted graph algorithms like Dijkstra.
  3. So shortest paths is not a trie use.

Why the other options are wrong

  • B. Autocomplete lists words below a prefix node.
  • C. Spell checkers look up words and nearby prefixes in a trie.
  • D. Routers use tries for longest prefix match.

Question 2Easy

The words tea, ten, to, inn, in, i are inserted into an empty trie (one node per character, root not counted). How many nodes does the trie have, excluding the root?

  1. 8
  2. 9
  3. 14
  4. 7

Answer: A. 8

Explanation

Shared prefixes share nodes. Counting each new character node gives 8, compared with 14 characters in total.

Step-by-step solution

  1. tea: t, e, a (3); ten: adds n (1); to: adds o (1).
  2. inn: i, n, n (3); in and i: already present (0).
  3. Total 3 + 1 + 1 + 3 = 8.

Why the other options are wrong

  • B. 9 counts a shared node twice.
  • C. 14 is the total number of characters.
  • D. 7 misses a node.

Tip: Count distinct prefixes, not characters.

Question 3Easy

A trie stores banana, band, bandana, ban, apple, bank, with each node keeping the count of words passing through it. What count is stored at the node for prefix "band"?

  1. 6
  2. 1
  3. 2
  4. 3

Answer: C. 2

Explanation

Words starting with "band": band, bandana, so the count is 2.

Step-by-step solution

  1. Words starting with band: band, bandana.
  2. banana diverges at the 4th letter (a vs d); ban and bank stop or diverge earlier.
  3. Count at the band node = 2.

Why the other options are wrong

  • A. 6 is the total number of words.
  • B. 1 counts only the exact word band; the count includes longer words too.
  • D. 3 wrongly includes banana, which goes ban-a.

Tip: Prefix count = number of words starting with that prefix.

Question 4Easy

What is the time complexity of inserting or searching a word of length L in a trie?

  1. O(log n)
  2. O(L log n)
  3. O(n)
  4. O(L)

Answer: D. O(L)

Explanation

Each character moves one level down, independent of how many words are stored.

Step-by-step solution

  1. Each character moves one level down.
  2. Each step is O(1) with an array or hash child map.
  3. Total O(L), independent of the number of words.

Why the other options are wrong

  • A. Tries do not use comparisons that would give log n.
  • B. No search among siblings is needed with direct child indexing, so no log n.
  • C. Time does not grow with the number of words n.

Tip: Trie ops: O(length of word).

Question 5Medium

A trie node uses an array of 26 child pointers. What is the main drawback?

  1. Slow lookups
  2. It cannot be traversed in order
  3. It cannot store duplicate words
  4. High memory use, since most pointers are null

Answer: D. High memory use, since most pointers are null

Explanation

Using a hash map per node or a compressed (radix) trie reduces the waste.

Step-by-step solution

  1. Each node reserves 26 pointers.
  2. Most nodes have only one or two real children.
  3. So most pointers are null, wasting memory.

Why the other options are wrong

  • A. Lookups are O(1) per character with an array.
  • B. Arrays indexed by letter make sorted traversal easy.
  • C. Duplicates are just a flag or counter; storage is not the drawback.

Tip: Array children: fast, memory-heavy. Map children: compact, slower.

Question 6Medium

The words car, cat, cart, dog, do are inserted into an empty trie (one node per character, root not counted). How many nodes does the trie have, excluding the root?

  1. 9
  2. 15
  3. 7
  4. 8

Answer: D. 8

Explanation

Shared prefixes share nodes. Counting each new character node gives 8, compared with 15 characters in total.

Step-by-step solution

  1. car: c, a, r (3 nodes).
  2. cat: shares c-a, adds t (1). cart: shares c-a-r, adds t (1).
  3. dog: d, o, g (3). do: fully shared (0).
  4. Total 3 + 1 + 1 + 3 = 8.

Why the other options are wrong

  • A. 9 counts one shared node twice.
  • B. 15 is the total number of characters, ignoring shared prefixes.
  • C. 7 misses a node.

Tip: Nodes = number of distinct prefixes.

Question 7Medium

What is the worst-case space of a trie storing n words of average length L over an alphabet of size k (array children)?

  1. O(n)
  2. O(k)
  3. O(L)
  4. O(n * L * k)

Answer: D. O(n * L * k)

Explanation

Without shared prefixes there can be n * L nodes, each with k pointers.

Step-by-step solution

  1. With no shared prefixes there are up to n x L nodes.
  2. Each node holds an array of k child pointers.
  3. Space O(n * L * k).

Why the other options are wrong

  • A. Nodes grow with total characters, not just n.
  • B. k is per node, not total.
  • C. L is one word's length; there are n words.

Tip: Space = (total characters) x (alphabet size) in the worst case.

Question 8Medium

What does this code print?

root = {}
for w in ["to", "tea", "ted", "ten", "i", "in", "inn"]:
    cur = root
    for ch in w:
        cur = cur.setdefault(ch, {})
    cur["$"] = True

def count(node):
    return sum(count(c) for k, c in node.items() if k != "$") + ("$" in node)

print(count(root["t"]), count(root["t"]["e"]))
  1. 3 4
  2. 4 4
  3. 4 3
  4. 7 3

Answer: C. 4 3

Explanation

count adds end-of-word markers in a subtree: under 't' are to, tea, ted, ten (4); under 'te' are tea, ted, ten (3).

Step-by-step solution

  1. count(node) = number of '$' markers in that subtree.
  2. Under 't': to, tea, ted, ten = 4.
  3. Under 't' -> 'e': tea, ted, ten = 3.
  4. Output: 4 3.

Why the other options are wrong

  • A. Swaps the two counts.
  • B. 'to' is under 't' but not under 'te'.
  • D. Counts all 7 words under 't', but i, in, inn are under 'i'.

Question 9Hard

To find the maximum XOR of a number x with any number in a set, a binary trie of the numbers is walked from the most significant bit. At each level it:

  1. Always goes to the 0 child
  2. Prefers the child with the opposite bit of x, if it exists
  3. Prefers the child with the same bit as x
  4. Visits both children

Answer: B. Prefers the child with the opposite bit of x, if it exists

Explanation

A differing high bit contributes more to the XOR than all lower bits combined.

Step-by-step solution

  1. Go from the highest bit down.
  2. If x has bit b, a number with the opposite bit makes the XOR 1 there.
  3. Take the opposite child if it exists, otherwise the same child.

Why the other options are wrong

  • A. Always choosing 0 ignores x's bits.
  • C. The same bit gives a 0 in the XOR.
  • D. Visiting both is unnecessary; the greedy choice is optimal.

Tip: Max XOR: walk a binary trie, prefer the opposite bit.

Question 10Hard

What does this code print?

root = {}
def insert(w):
    cur = root
    for ch in w:
        cur = cur.setdefault(ch, {})
    cur["$"] = True

def depth(node):
    kids = [depth(c) for k, c in node.items() if k != "$"]
    return 1 + max(kids) if kids else 0

for w in ["algo", "all", "also", "assoc", "tree", "trie"]:
    insert(w)
print(depth(root), len(root), len(root["a"]))
  1. 4 2 1
  2. 5 2 2
  3. 5 6 2
  4. 5 2 1

Answer: B. 5 2 2

Explanation

The height equals the longest word (assoc, 5);
the root has children 'a' and 't';
under 'a' the children are 'l' and 's'.

Step-by-step solution

  1. depth = number of edges on the longest root-to-leaf path = longest word length: 'assoc' has 5.
  2. Root children: 'a' and 't', so len(root) = 2.
  3. Under 'a': 'l' (algo, all, also) and 's' (assoc): 2.

Why the other options are wrong

  • A. Longest word has 5 letters, not 4.
  • C. len(root) counts first letters, not words.
  • D. 'a' has two children, 'l' and 's'.

Practise all 30 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics