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?
- A. 3
- B. 4
- C. 2
- 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.
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
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [B, C, G]; B: [A, E, G]; C: [A, G]; D: [F, G]; E…
- A disjoint-set (union-find with union by rank and path compression) over 7 elements (0 to 6), each initially its own component, runs this…
- Keys 55, 65, 75 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 4, 49, 97, 15, 7, 96, 24, 50 are inserted in this order into an empty BST. Using the BST-specific LCA algorithm (walk down from the…
- Keys 38, 28, 18 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- An undirected graph has edges (u–v: weight) A–C:16, B–C:30, C–D:17, A–D:2, A–B:3, B–D:12. What is the weight of its minimum spanning tree?