ProgrammingHardGATE CS & IT · Infosys
What is the time complexity of building a binary heap from n unordered items with heapq.heapify?
- A. O(n)
- B. O(n log n)
- C. O(log n)
- D. O(n^2)
Correct answer: A. O(n)
Bottom-up heapify sifts down each node by an amount that depends on its height, and the total works out to O(n). Pushing the items one by one with heappush would cost O(n log n).
More Python Programming questions
- Which statement about bisect.insort(a, x) on a plain Python list a is true?
- What is printed? python import itertools print(list(itertools.accumulate([3, 1, 4], initial=10)))
- What is printed? python from itertools import groupby s = 'aabbaa' print([(k, len(list(g))) for k, g in groupby(s)])
- What is printed? python print(sorted(['b', 'A', 'a', 'B'], key=str.lower))
- What is printed? python nums = map(int, '1 2 3'.split()) a = list(nums) b = list(nums) print(a, b)
- What is printed? python print(1020 + 1 - 1020, float(1020 + 1) - float(1020))