Data Structures & AlgorithmsVery hardProduct-company MCQ pattern
A trie is built from the words {cat, card, den, cats, dog, do, cap}. 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. 1
- C. 5
- D. 2
Correct answer: D. 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, 1, 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
- Build a binary min-heap in place from the array [2, 63, 36, 74, 23, 20, 15, 22] using bottom-up heapify (sift-down from the last internal…
- Build a binary min-heap in place from the array [47, 72, 84, 82, 36, 61, 51, 7, 2] using bottom-up heapify (sift-down from the last…
- Undirected graph edges: E–C, C–A, B–E, B–C, A–B, A–E, C–D. Using a BFS 2-colouring, is this graph bipartite?
- 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…
- 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…