Question 1 
What is the time complexity of Build Heap operation. Build Heap is used to build a max(or min) binary heap from a given array. Build Heap is used in Heap Sort as a first step for sorting.
O(nLogn)  
O(n^2)  
O(Logn)  
O(n) 
Discuss it
Question 1 Explanation:
Following is algorithm for building a Heap of an input array A.
BUILDHEAP(A) heapsize := size(A); for i := floor(heapsize/2) downto 1 do HEAPIFY(A, i); end for ENDAlthough the worst case complexity looks like O(nLogn), upper bound of time complexity is O(n). See following links for the proof of time complexity. http://www.dgp.utoronto.ca/people/JamesStewart/378notes/08buildheap/
Question 2 
Suppose we are sorting an array of eight integers using heapsort, and we have just finished some heapify (either maxheapify or minheapify) operations. The array now looks like this:
16 14 15 10 12 27 28
How many heapify operations have been performed on root of heap?
1  
2  
3 or 4  
5 or 6 
Discuss it
Question 2 Explanation:
In Heapsort, we first build a heap, then we do following operations till the heap size becomes 1.
a) Swap the root with last element
b) Call heapify for root
c) reduce the heap size by 1.
In this question, it is given that heapify has been called few times and we see that last two elements in given array are the 2 maximum elements in array. So situation is clear, it is maxheapify whic has been called 2 times.
Question 3 
A maxheap is a heap where the value of each parent is greater than or equal to the values of its children. Which of the following is a maxheap? (GATE CS 2011)
A  
B  
C  
D 
Discuss it
Question 3 Explanation:
See http://www.geeksforgeeks.org/datastructuresandalgorithmsset26/ for explanation.
Question 4 
A 3ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.
Which one of the following is a valid sequence of elements in an array representing 3ary max heap?
1, 3, 5, 6, 8, 9  
9, 6, 3, 1, 8, 5  
9, 3, 6, 8, 5, 1  
9, 5, 6, 8, 3, 1 
Discuss it
Question 4 Explanation:
Following 3ary Max Heap can be constructed from sequence given option (D)
9 /  \ /  \ 5 6 8 /  /  3 1
Question 5 
Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the valid 3 ary max heap found in the above question, Which one of the following is the sequence of items in the array representing the resultant heap?
10, 7, 9, 8, 3, 1, 5, 2, 6, 4  
10, 9, 8, 7, 6, 5, 4, 3, 2, 1  
10, 9, 4, 5, 7, 6, 8, 2, 1, 3  
10, 8, 6, 9, 7, 2, 3, 4, 1, 5 
Discuss it
Question 5 Explanation:
After insertion of 7
9 /  \ /  \ 7 6 8 /  \ /  \ 3 1 5After insertion of 2
9 /  \ /  \ 7 6 8 /  \ / /  \ / 3 1 5 2After insertion of 10
10 /  \ /  \ 7 9 8 /  \ /  /  \ /  3 1 5 2 6After insertion of 4
10 /  \ /  \ 7 9 8 /  \ /  \ /  \ /  \ 3 1 5 2 6 4
Question 6 
Consider a binary maxheap implemented using an array. Which one of the following array represents a binary maxheap? (GATE CS 2009)
25,12,16,13,10,8,14  
25,12,16,13,10,8,14  
25,14,16,13,10,8,12  
25,14,12,13,10,8,16 
Discuss it
Question 6 Explanation:
A tree is maxheap if data at every node in the tree is greater than or equal to it’s children’ s data.
In array representation of heap tree, a node at index i has its left child at index 2i + 1 and right child at index 2i + 2.
25 / \ / \ 14 16 / \ / \ / \ / \ 13 10 8 12
Question 7 
What is the content of the array after two delete operations on the correct answer to the previous question?
14,13,12,10,8  
14,12,13,8,10  
14,13,8,12,10  
14,13,12,8,10 
Discuss it
Question 7 Explanation:
For Heap trees, deletion of a node includes following two operations.
1) Replace the root with last element on the last level.
2) Starting from root, heapify the complete tree from top to bottom..
Let us delete the two nodes one by one:
1) Deletion of 25:
Replace 25 with 12
12 / \ / \ 14 16 / \ / / \ / 13 10 8Since heap property is violated for root (16 is greater than 12), make 16 as root of the tree.
16 / \ / \ 14 12 / \ / / \ / 13 10 82) Deletion of 16: Replace 16 with 8
8 / \ / \ 14 12 / \ / \ 13 10Heapify from root to bottom.
14 / \ / \ 8 12 / \ / \ 13 10 14 / \ / \ 13 12 / \ / \ 8 10
Question 8 
We have a binary heap on n elements and wish to insert n more elements (not necessarily one after another) into this heap. The total time required for this is
(A) (logn)
(B) (n)
(C) (nlogn)
(D) (n^2)
A  
B  
C  
D 
Discuss it
Question 8 Explanation:
We can reduce the problem to Build Heap for 2n elements. Time taken for build heap is O(n)
Question 9 
In a minheap with n elements with the smallest element at the root, the 7th smallest element can be found in time
a) (n log n)
b) (n)
c) (log n)
d) (1)
The question was not clear in original GATE exam. For clarity, assume that there are no duplicates in MinHeap and accessing heap elements below root is allowed.
a  
b  
c  
d 
Discuss it
Question 9 Explanation:
The 7th smallest element must be in first 7 levels. Total number of nodes in any Binary Heap in first 7 levels is at most 1 + 2 + 4 + 8 + 16 + 32 + 64 which is a constant. Therefore we can always find 7th smallest element in [Tex]\theta(1)[/Tex] time.
If MinHeap is allowed to have duplicates, then time complexity becomes Θ(Log n).
Also, if MinHeap doesn't allow directly accessing elements below root and supports only extractmin() operation, then also time complexity becomes Θ(Log n).
Question 10 
In a binary max heap containing n numbers, the smallest element can be found in time (GATE CS 2006)
0(n)  
O(logn)  
0(loglogn)  
0(1) 
Discuss it
Question 10 Explanation:
In a max heap, the smallest element is always present at a leaf node. So we need to check for all leaf nodes for the minimum value. Worst case complexity will be O(n)
12 / \ / \ 8 7 / \ / \ / \ / \ 2 3 4 5
Question 11 
The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap is.
a  
b  
c  
d 
Discuss it
Question 11 Explanation:
32, 15, 20, 30, 12, 25, 16 After insertion of 32, 15 and 20 32 / \ 15 20 After insertion of 30 32 / \ 15 20 / 30 Max Heap property is violated, so 30 is swapped with 15 32 / \ 30 20 / 15 After insertion of 12 32 / \ 30 20 / \ 15 12 After insertion of 25 32 / \ 30 20 / \ / 15 12 25 Max Heap property is violated, so 25 is swapped with 20 32 / \ 30 25 / \ / 15 12 20 After insertion of 16 32 / \ 30 25 / \ / \ 15 12 20 16
Question 12 
Given two max heaps of size n each, what is the minimum possible time complexity to make a one maxheap of size from elements of two max heaps?
O(nLogn)  
O(nLogLogn)  
O(n)  
O(nLogn) 
Discuss it
Question 12 Explanation:
We can build a heap of 2n elements in O(n) time. Following are the steps.
Create an array of size 2n and copy elements of both heaps to this array.
Call build heap for the array of size 2n. Build heap operation takes O(n) time.
Question 13 
A priority queue is implemented as a MaxHeap. Initially, it has 5 elements. The levelorder traversal of the heap is: 10, 8, 5, 3, 2. Two new elements 1 and 7 are inserted into the heap in that order. The levelorder traversal of the heap after the insertion of the elements is:
10, 8, 7, 3, 2, 1, 5  
10, 8, 7, 2, 3, 1, 5  
10, 8, 7, 1, 2, 3, 5  
10, 8, 7, 5, 3, 2, 1 
Discuss it
Question 13 Explanation:
Initially heap has 10, 8, 5, 3, 2 10 / \ 8 5 / \ 3 2 After insertion of 1 10 / \ 8 5 / \ / 3 2 1 No need to heapify as 5 is greater than 1. After insertion of 7 10 / \ 8 5 / \ / \ 3 2 1 7 Heapify 5 as 7 is greater than 5 10 / \ 8 7 / \ / \ 3 2 1 5 No need to heapify any further as 10 is greater than 7
Question 14 
Which of the following Binary Min Heap operation has the highest time complexity?
Inserting an item under the assumption that the heap has capacity to accommodate one more item  
Merging with another heap under the assumption that the heap has capacity to accommodate items of other heap  
Deleting an item from heap  
Decreasing value of a key 
Discuss it
Question 14 Explanation:
The merge operation takes O(n) time, all other operations given in question take O(Logn) time.
The Binomial and Fibonacci Heaps do merge in better time complexity.
Question 15 
Consider any array representation of an n element binary heap where the elements are stored from index 1 to index n of the array. For the element stored at index i of the array (i <= n), the index of the parent is
i  1  
floor(i/2)  
ceiling(i/2)  
(i+1)/2 
Discuss it
Question 15 Explanation:
Binary heaps can be represented using arrays: storing elements in an array and using their relative positions within the array to represent childparent relationships.
For the binary heap element stored at index i of the array,
Parent Node will be at index: floor(i/2)
Left Child will be at index: 2i
Right child will be at index: 2*i + 1
This explanation is contributed by Saksham Seth.
Question 16 
Consider a max heap, represented by the array: 40, 30, 20, 10, 15, 16, 17, 8, 4. Now consider that a value 35 is inserted into this heap. After insertion, the new heap is
40, 30, 20, 10, 15, 16, 17, 8, 4, 35  
40, 35, 20, 10, 30, 16, 17, 8, 4, 15  
40, 30, 20, 10, 35, 16, 17, 8, 4, 15  
40, 35, 20, 10, 15, 16, 17, 8, 4, 30 
Discuss it
Question 16 Explanation:
The array 40, 30, 20, 10, 15, 16, 17, 8, 4 represents following heap
40 / \ 30 20 / \ / \ 10 15 16 17 / \ 8 4After insertion of 35, we get
40 / \ 30 20 / \ / \ 10 15 16 17 / \ / 8 4 35After swapping 35 with 15 and swapping 35 again with 30, we get
40 / \ 35 20 / \ / \ 10 30 16 17 / \ / 8 4 15
Question 17 
Consider the following array of elements. 〈89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100〉. The minimum number of interchanges needed to convert it into a maxheap is
4  
5  
2  
3 
Discuss it
Question 17 Explanation:
〈89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100〉
89 / \ 19 50 / \ / \ 17 12 15 2 / \ / \ / \ 5 7 11 6 9 100 Minimum number of swaps required to convert above tree to Max heap is 3. Below are 3 swap operations. Swap 100 with 15 Swap 100 with 50 Swap 100 with 89 100 / \ 19 89 / \ / \ 17 12 50 5 / \ / \ / \ 7 11 6 9 2 15
Question 18 
An operator delete(i) for a binary heap data structure is to be designed to delete the item in the ith node. Assume that the heap is implemented in an array and i refers to the ith index of the array. If the heap tree has depth d (number of edges on the path from the root to the farthest leaf), then what is the time complexity to refix the heap efficiently after the removal of the element?
O(1)  
O(d) but not O(1)  
O(2^{d}) but not O(d)  
O(d2^{d}) but not O(2^{d})

Discuss it
Question 18 Explanation:
For this question, we have to slightly tweak the delete_min() operation of the heap data structure to implement the delete(i) operation. The idea is to empty the spot in the array at the index i (the position at which element is to be deleted) and replace it with the last leaf in the heap (remember heap is implemented as complete binary tree so you know the location of the last leaf), decrement the heap size and now starting from the current position i (position that held the item we deleted), shift it up in case newly replaced item is greater than the parent of old item (considering maxheap). If it’s not greater than the parent, then percolate it down by comparing with the child’s value. The newly added item can percolate up/down a maximum of d times which is the depth of the heap data structure.
Thus we can say that complexity of delete(i) would be O(d) but not O(1).
http://geeksquiz.com/binaryheap/
This solution is contributed by Pranjul Ahuja.
Question 19 
A complete binary minheap is made by including each integer in [1, 1023] exactly once. The depth of a node in the heap is the length of the path from the root of the heap to that node. Thus, the root is at depth 0. The maximum depth at which integer 9 can appear is _____________
[This question was originally asked as FillintheBlanks question]
6  
7  
8  
9 
Discuss it
Question 19 Explanation:
here node with integer 1 has to be at root only. Now for maximum depth of the tree the following arrangement can be taken. Take root as level 1.
make node 2 at level 2 as a child node of node 1.
make node 3 at level 3 as the child node of node 2.
..
.. and so on for nodes 4,5,6,7
..
make node 8 at level 8 as the child node of node 7.
make node 9 at level 9 as the child node of node 8.
Putting other nodes properly, this arrangement of the the complete binary tree will follow the property of min heap.
So total levels are 9. node 9 is at level 9 and depth of node 9 is 8 from the root.
Question 20 
Which of the following sequences of array elements forms a heap?
{23, 17, 14, 6, 13, 10, 1, 12, 7, 5}  
{23, 17, 14, 6, 13, 10, 1, 5, 7, 12}  
{23, 17, 14, 7, 13, 10, 1, 5, 6, 12}  
{23, 17, 14, 7, 13, 10, 1, 12, 5, 7} 
Discuss it
Question 20 Explanation:
When they are asking for heap, by default it's max heap.
Basic Requirement: Array representation of binary tree
Starting from basics lets first understand heap trees We have 2 types of heap – Min heap and Max heap
In Min heap the parent is always smaller than its children and in Max heap parent is always greater than its children.
Looking at the options we can tell that which tree is Max heap tree. Now consider each option one by one and draw a tree
From options it is clear that only option C satisfies the Max heap tree property.
This explanation has been contributed by Parul Sharma.
There are 20 questions to complete.