Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
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 standard bottom-up heapify approach?
- A. O(n log n)
- B. O(log n)
- C. O(n^2)
- D. O(n)
Correct answer: D. O(n)
Although each of the roughly n/2 internal nodes could sift down O(log n) levels, most nodes are near the bottom of the heap and sift down only a little; summing the work across all levels gives a total of O(n), not O(n log n).
More Coding Interview Problems questions
- 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) =…
- Problem: Which statement about the time complexity of checking if two strings are anagrams is most efficient and correct?
- Problem: For the 'minimum number of platforms needed for a railway station given arrival and departure times' problem, what is the…