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?
- A. 7
- B. 6
- C. 20
- 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.
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
- Directed graph on vertices A, B, C, D, E, F with edges F→C, B→A, C→F, D→B, B→F, A→E, E→F, A→C, B→D (no weights). What is the number of…
- Array A = [3, 4, 4, 1, 1, 4, 5, 5]. An increasing subsequence is chosen by INDEX positions i1 < i2 < … with A[i1] < A[i2] < … (strictly…
- Directed graph on vertices A, B, C, D, E, F, G with edges E→B, D→A, C→A, B→E, C→B, F→A, E→D, G→B, B→F, F→G. How many strongly connected…
- Keys [20, 43, 1, 35, 37, 18, 49] are inserted, in this order, into an empty AVL tree using the standard rebalancing (left, right…
- On an 7 × 7 chessboard with rows and columns numbered 0 to 6, a knight starts at (4, 5) and must reach (6, 1) (as (row, column)). A knight…
- Distinct words ['c', 'bcb', 'cccb', 'ba', 'bcba', 'bb', 'ac'] are inserted into an empty trie (one node per character, shared prefixes…