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?

  1. A. No operation is faster than a hash set for this
  2. B. Prefix search in O(n) by scanning every stored word
  3. C. Prefix search: O(length of the query string), independent of how many words are stored
  4. 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

Exams that ask this topic