Data Structures & AlgorithmsModerateGATE CS · ICPC regional

Words ['aaac', 'bac', 'cb', 'ac', 'baab', 'bc', 'bac'] are inserted in this order into an empty trie with one node per character, shared prefixes sharing nodes, and a root. (A repeated word adds no nodes.) How many nodes does the trie have, not counting the root?

  1. A. 7
  2. B. 6
  3. C. 20
  4. D. 13

Correct answer: D. 13

Every distinct non-empty prefix is exactly one node, so there are 13 nodes. Counting each word's length, or each word, double-counts shared prefixes.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic