Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern

A trie is built from the words {cat, card, dot, den, deed, dog}. Using the trie (walk the prefix's characters, then count all words in the subtree below), how many of these words have "do" as a prefix?

  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 0

Correct answer: C. 2

Walking down the trie along the characters of "do" reaches a node whose subtree contains exactly the words starting with that prefix; counting end-of-word markers in that subtree gives 2. Counting only exact matches equal to "do" itself, 0, misses the longer words that merely start with it.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic