Question 71
HardWhat is the index of the parent of node i in a 1-indexed array heap?
- A.
2i
- B.
⌊i/2⌋
- C.
(i − 1)/2
- D.
i − 1
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the index of the parent of node i in a 1-indexed array heap?
2i
⌊i/2⌋
(i − 1)/2
i − 1
Sign up free to see the answer and a step-by-step explanation.
In a min-heap of n distinct elements, where can the maximum be?
Among the leaves, the last ⌈n/2⌉ positions
Anywhere
At the root
At index 1
Sign up free to see the answer and a step-by-step explanation.
How many leaves does a heap with 25 elements have?
8
16
12
13
Sign up free to see the answer and a step-by-step explanation.
What is a d-ary heap's trade-off compared with a binary heap?
No trade-off
It can't be stored in an array
Shallower tree (faster insert and decrease-key) but more comparisons per sift-down
Deeper tree
Sign up free to see the answer and a step-by-step explanation.
Which problem is efficiently solved with a heap: 'given meeting times, find the minimum number of rooms'?
Use a stack
Use a hash map only
Sort by start and keep a min-heap of end times
Use BFS
Sign up free to see the answer and a step-by-step explanation.
What does heapq.nlargest(k, data) cost for n items?
O(k)
O(n log k)
O(n log n)
O(n²)
Sign up free to see the answer and a step-by-step explanation.
Why is heap sort not stable?
It doesn't compare
It uses recursion
Sift-downs move equal elements past each other
It is stable
Sign up free to see the answer and a step-by-step explanation.
A leftist heap or skew heap is mainly designed to support which operation efficiently?
Sorting strings
Searching
Range queries
Merging two heaps in O(log n)
Sign up free to see the answer and a step-by-step explanation.
In Dijkstra with a binary heap and lazy deletion, why might a vertex be popped more than once?
Old entries with larger distances stay in the heap and are skipped when popped
Graphs have loops
It is never popped twice
Heaps duplicate entries
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of finding the k-th smallest element by building a min-heap and popping k times?
O(k)
O(n + k log n)
O(nk)
O(n log n)
Sign up free to see the answer and a step-by-step explanation.