Question 51
MediumIn a d-ary heap (each node has d children), what is the height for n elements?
- 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 a d-ary heap (each node has d children), what is the height for n elements?
About log_d n
About d log n
About n/d
About √n
Sign up free to see the answer and a step-by-step explanation.
What does decrease-key do in a min-heap of size n, and how long does it take?
Sorts the heap in O(n log n)
Increases all keys in O(n)
Deletes the key in O(1)
Lowers a key and sifts it up, in O(log n)
Sign up free to see the answer and a step-by-step explanation.
Heap sort on an array of n elements uses how much extra space?
O(n)
O(n log n)
O(1)
O(log n) for the heap
Sign up free to see the answer and a step-by-step explanation.
Which Python call turns a list into a heap in place in O(n)?
heapq.heapify(lst)
heapq.heappush(lst)
sorted(lst)
lst.sort()
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import heapq
print(list(heapq.merge([1, 4], [2, 5], [3, 6])))
[1, 3, 5, 2, 4, 6]
[1, 2, 3, 4, 5, 6]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import heapq
lo, hi, out = [], [], []
for x in [1, 2, 3]:
heapq.heappush(lo, -x)
heapq.heappush(hi, -heapq.heappop(lo))
if len(hi) > len(lo):
heapq.heappush(lo, -heapq.heappop(hi))
out.append(float(-lo[0]) if len(lo) > len(hi) else (-lo[0] + hi[0]) / 2)
print(out)
[1, 2, 3]
[1.0, 1.5, 2.0]
Sign up free to see the answer and a step-by-step explanation.
A max-heap is stored as [50, 30, 40, 10, 20, 35]. After inserting 45, what is the array?
[50, 45, 40, 10, 20, 35, 30]
[50, 30, 45, 10, 20, 35, 40]
[45, 50, 40, 10, 20, 35, 30]
[50, 30, 40, 10, 20, 35, 45]
Sign up free to see the answer and a step-by-step explanation.
Why is Dijkstra's algorithm usually implemented with a min-heap?
To repeatedly extract the unvisited vertex with the smallest tentative distance in O(log V)
To sort the vertices alphabetically
To store the graph's edges
To detect negative cycles
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of heap sort, and is it stable?
O(n log n), and it is not stable
O(n²), and it is not stable
O(n), and it is stable
O(n log n), and it is stable
Sign up free to see the answer and a step-by-step explanation.
Which data structure supports decrease-key in O(1) amortised time, improving Dijkstra's theoretical bound to O(E + V log V)?
Hash table
AVL tree
Fibonacci heap
Binary heap
Sign up free to see the answer and a step-by-step explanation.
[6, 5, 4, 3, 2, 1]
[1, 4, 2, 5, 3, 6]
[1.5, 2.0, 2.5]
[1.0, 2.0, 3.0]