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?
- A. O(n^2) time always
- B. O(n) time regardless of word length
- C. O(L) time, O(L) space (one node per unique character path, shared across common prefixes)
- 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
- Problem: A problem is solved via binary search on the answer (not on an array index) -- e.g. 'minimum capacity to ship packages within D…
- Problem: Which of these is the correct worst-case time complexity of building a max-heap from an array of n unsorted elements using the…
- Problem: In a graph represented with an adjacency list, what is the space complexity, and how does it compare to an adjacency matrix for a…
- Problem: Which best explains why the greedy 'interval scheduling to maximize the number of non-overlapping intervals' problem is solved…
- Problem: Which is the correct worst-case time complexity for inserting into a balanced BST (like a Red-Black or AVL tree) with n nodes…
- Problem: What is the time complexity of the standard recursive merge sort, including the merge step, expressed via its recurrence T(n) =…