Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

What is the time complexity of searching for a word of length m in a trie that stores n words, independent of n?

  1. A. O(m)
  2. B. O(n)
  3. C. O(n log n)
  4. D. O(m log n)

Correct answer: A. O(m)

A trie walks one edge per character of the query, so lookup is O(m), regardless of how many words are stored.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic