Data Structures & AlgorithmsVery hardProduct-company placement
A trie is built from the words {den, dog, car, card, do, cat, deer}. 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. 5
- C. 1
- 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 [32, 59, 35, 72, 53, 57, 16, 42, 15] using bottom-up heapify (sift-down from the last…
- Keys 47, 33, 29, 18, 55, 67 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a table of…
- Undirected graph edges: A–B, C–B, A–D, D–B, C–D, C–A. Using a BFS 2-colouring, is this graph bipartite?
- A disjoint-set (union-find with union by rank and path compression) over 9 elements (0 to 8), each initially its own component, runs this…
- Directed edges: A→B (5), A→C (6), C→B (-3), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…
- An undirected connected graph has edges A–B, B–C, C–D, D–E, E–F, F–G, G–H, D–F, F–H. How many articulation points (cut vertices) and how…