Data Structures & AlgorithmsModerateWipro · Capgemini pattern
The words bat, bath, bad, ban, band are inserted into an empty trie (one node per character, plus the root). How many nodes does the trie have, including the root?
- A. 8
- B. 7
- C. 17
- D. 9
Correct answer: A. 8
Root → b → a is shared by all five words (2 nodes). Under 'a' hang t, d and n (3 nodes). 'h' extends bat to bath and a second 'd' extends ban to band (2 nodes). 2 + 3 + 2 = 7 character nodes + the root = 8. '7' forgets the root; '17' is the total number of characters with no prefix sharing.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 65, 10 are inserted into an empty BST. How many nodes have EXACTLY one child?
- A climber can take 1 or 3 steps at a time. In how many different ordered ways can a staircase of 7 steps be climbed? (ways(n) = ways(n −…
- Keys 50, 40, 30, 45, 47 are inserted in that order into an empty AVL tree. What is the preorder traversal of the final tree?
- Kruskal's algorithm runs on edges (weight): A–B 4, A–C 3, B–C 2, B–D 5, C–D 7, C–E 8, D–E 6, D–F 9, E–F 4. What is the total weight of the…
- Bellman–Ford from S relaxes the directed edges in this fixed order in every pass: C→D (1), B→C (2), A→B (3), S→A (4). How many passes are…
- A min-heap of size k = 3 tracks the 3 largest values of the stream 5, 1, 9, 3, 7, 8, 2 (push each value; if the size exceeds 3, pop the…