Data Structures & AlgorithmsHardICPC regional · Product Company SDE

Problem: What is the time complexity of building a trie from n words with total character count L, and what is the space complexity?

  1. A. O(n^2) time always
  2. B. O(n) time regardless of word length
  3. C. O(L) time, O(L) space (one node per unique character path, shared across common prefixes)
  4. D. O(L log L) time, O(1) space

Correct answer: C. O(L) time, O(L) space (one node per unique character path, shared across common prefixes)

Each character of each word is processed once while walking/creating trie nodes, giving O(L) time; the number of nodes created is bounded by the total number of distinct character positions across all words, giving O(L) space (less if prefixes are shared).

More Coding Interview Problems questions

Exams that ask this topic