Data Structures & AlgorithmsVery hardIBM · Deloitte pattern
A trie is built from the words {den, deed, do, cat, cap, dot}. Using the trie (walk the prefix's characters, then count all words in the subtree below), how many of these words have "c" as a prefix?
- A. 4
- B. 2
- C. 3
- D. 0
Correct answer: B. 2
Walking down the trie along the characters of "c" 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 "c" 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
- A binary tree has preorder traversal [11, 83, 45, 67, 61, 47, 95] and inorder traversal [11, 45, 47, 61, 67, 83, 95]. Reconstructing the…
- Keys 41, 31, 21 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 38, 48, 58 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Undirected graph edges: B–D, C–A, E–B, E–A, D–E, C–B, A–B. Using a BFS 2-colouring, is this graph bipartite?
- A directed graph has edges E→A, C→D, D→B, C→E, D→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [B]; B: [A, C, F]; C: [B, E, F]; D: [F]; E: [C]; F…