Question 11
EasyIn an array-based binary heap (0-indexed), where are the children of the element at index i?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
In an array-based binary heap (0-indexed), where are the children of the element at index i?
At 2i and 2i + 1
At i/2 and i/2 + 1
At 2i + 1 and 2i + 2
At i + 1 and i + 2
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of removing the minimum from a binary min-heap of n elements?
O(n)
O(1)
O(n log n)
O(log n)
Sign up free to see the answer and a step-by-step explanation.
After calls to heapq.heappush, at which index of the list is the smallest element?
0
The middle index
The last index
Any index
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import heapq
h = [5, 1, 8, 3]
heapq.heapify(h)
print(h[0])
5
1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import heapq
print(heapq.nsmallest(2, [7, 2, 9, 4]))
[2, 4]
[9, 7]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import heapq
h = []
for x in [4, 1, 3]:
heapq.heappush(h, x)
print(heapq.heappop(h), heapq.heappop(h))
4 1
1 4
Sign up free to see the answer and a step-by-step explanation.
What is the height of a binary heap with n elements?
About log₂ n
n/2
n
√n
Sign up free to see the answer and a step-by-step explanation.
In a heap, what does sift-down (heapify-down) do?
Deletes the lowest element
Adds a new leaf
Moves an element down, swapping with a child, until the heap property holds
Sorts the whole array
Sign up free to see the answer and a step-by-step explanation.
In a min-heap, what does sift-up do after an insert?
Removes the new element
Moves the root to the bottom
Doubles the array
Moves the new element up while it is smaller than its parent
Sign up free to see the answer and a step-by-step explanation.
Standard in-place heap sort produces ascending order using which kind of heap?
A max-heap
Either, with the same steps
No heap
A min-heap
Sign up free to see the answer and a step-by-step explanation.
8
3
[2, 7]
[7, 2]
3 1
1 3