Data Structures & AlgorithmsHardCAT · GATE CS
Problem: A trie (prefix tree) storing n words with total length L supports which operation's time complexity most efficiently?
- A. No operation is faster than a hash set for this
- B. Prefix search in O(n) by scanning every stored word
- C. Prefix search: O(length of the query string), independent of how many words are stored
- D. Insertion in O(n log n) per word
Correct answer: C. Prefix search: O(length of the query string), independent of how many words are stored
A trie walks one character at a time following child pointers, so searching for (or inserting) a string of length k takes O(k) regardless of how many other words share the trie, which a plain hash set of whole words cannot do for prefix queries.
More Coding Interview Problems questions
- Problem: Floyd's cycle detection ('tortoise and hare') finds whether a linked list has a cycle. What are its time and space complexity?
- Problem: What is the time complexity of a segment tree's range-query and point-update operations on an array of size n?
- Problem: Finding the median of two sorted arrays of sizes m and n can be done faster than merging them. What is the best known time…
- Problem: Which statement about the backtracking solution to generating all subsets of an n-element set is correct?
- Problem: Generating all permutations of an n-element array via backtracking takes how much time, and why?
- Problem: In the N-Queens backtracking search, what pruning technique keeps the search from degenerating into a full O(N^N) brute force?