Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2

Distinct words ['baab', 'bcb', 'cbaa', 'ccba', 'bbaa', 'ab', 'aac'] are inserted into an empty trie (one node per character, shared prefixes sharing nodes, root at depth 0). How many nodes are at depth exactly 2?

  1. A. 6
  2. B. 7
  3. C. 10
  4. D. 1

Correct answer: B. 7

Depth d holds one node per distinct prefix of length 2, so the count is 7. Counting only the words of that length misses the shared prefixes.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic