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.
A
O(nLogn)
B
O(n^2)
C
O(Logn)
D
O(n)
Heap    
Discuss it


Question 1 Explanation: 
Following is algorithm for building a Heap of an input array A.
BUILD-HEAP(A) 
    heapsize := size(A); 
    for i := floor(heapsize/2) downto 1 
        do HEAPIFY(A, i); 
    end for 
END
Although 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?
A
1
B
2
C
3 or 4
D
5 or 6
Sorting    Heap    HeapSort    
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 max-heap 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 max-heap? (GATE CS 2011)
A
A
B
B
C
C
D
D
Heap    
Discuss it


Question 3 Explanation: 
Question 4
A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary 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 3-ary 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 3-ary max heap?
A
1, 3, 5, 6, 8, 9
B
9, 6, 3, 1, 8, 5
C
9, 3, 6, 8, 5, 1
D
9, 5, 6, 8, 3, 1
Heap    
Discuss it


Question 4 Explanation: 
Following 3-ary 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?
A
10, 7, 9, 8, 3, 1, 5, 2, 6, 4
B
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
C
10, 9, 4, 5, 7, 6, 8, 2, 1, 3
D
10, 8, 6, 9, 7, 2, 3, 4, 1, 5
Heap    
Discuss it


Question 5 Explanation: 
After insertion of 7
                                          9
                                      /   |   \
                                    /     |     \
                                  7       6       8
                               / | \
                             /   |  \
                            3    1    5    
After insertion of 2
                                           9
                                      /    |   \
                                    /      |     \
                                  7        6       8
                               / | \       /
                             /   |  \     /
                            3    1    5  2
After insertion of 10
                                 10
                             /    |   \
                           /      |     \
                        7         9       8
                    / | \       / |
                  /   |  \     /  |
                3    1    5  2    6
After insertion of 4
                                 10
                             /   |   \
                           /     |     \
                         7        9       8
                      / | \      / | \
                    /   |  \    /  |   \
                  3    1    5  2   6    4
Question 6
Consider a binary max-heap implemented using an array. Which one of the following array represents a binary max-heap? (GATE CS 2009)
A
25,12,16,13,10,8,14
B
25,12,16,13,10,8,14
C
25,14,16,13,10,8,12
D
25,14,12,13,10,8,16
Heap    
Discuss it


Question 6 Explanation: 
A tree is max-heap 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?
A
14,13,12,10,8
B
14,12,13,8,10
C
14,13,8,12,10
D
14,13,12,8,10
Heap    
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    8
Since heap property is violated for root (16 is greater than 12), make 16 as root of the tree.
           16
        /     \
      /        \
    14         12
   / \         /
  /   \       /
13     10    8
2) Deletion of 16: Replace 16 with 8
           8
        /    \
       /      \
    14         12
   / \
  /   \
 13     10
Heapify 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) \theta(logn) (B) \theta(n) (C) \theta(nlogn) (D) \theta(n^2)
A
A
B
B
C
C
D
D
Heap    
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 min-heap with n elements with the smallest element at the root, the 7th smallest element can be found in time a) \theta(n log n) b) \theta(n) c) \theta(log n) d) \theta(1) The question was not clear in original GATE exam. For clarity, assume that there are no duplicates in Min-Heap and accessing heap elements below root is allowed.
A
a
B
b
C
c
D
d
Heap    
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 Min-Heap is allowed to have duplicates, then time complexity becomes Θ(Log n). Also, if Min-Heap doesn't allow directly accessing elements below root and supports only extract-min() 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)
A
0(n)
B
O(logn)
C
0(loglogn)
D
0(1)
Heap    
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. tree
A
a
B
b
C
c
D
d
Heap    
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 max-heap of size from elements of two max heaps?
A
O(nLogn)
B
O(nLogLogn)
C
O(n)
D
O(nLogn)
Heap    
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 Max-Heap. Initially, it has 5 elements. The level-order 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 level-order traversal of the heap after the insertion of the elements is:
A
10, 8, 7, 3, 2, 1, 5
B
10, 8, 7, 2, 3, 1, 5
C
10, 8, 7, 1, 2, 3, 5
D
10, 8, 7, 5, 3, 2, 1
Heap    GATE-CS-2014-(Set-2)    
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?
A
Inserting an item under the assumption that the heap has capacity to accommodate one more item
B
Merging with another heap under the assumption that the heap has capacity to accommodate items of other heap
C
Deleting an item from heap
D
Decreasing value of a key
Heap    
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
A
i - 1
B
floor(i/2)
C
ceiling(i/2)
D
(i+1)/2
Heap    GATE-CS-2001    
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 child-parent 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
A
40, 30, 20, 10, 15, 16, 17, 8, 4, 35
B
40, 35, 20, 10, 30, 16, 17, 8, 4, 15
C
40, 30, 20, 10, 35, 16, 17, 8, 4, 15
D
40, 35, 20, 10, 15, 16, 17, 8, 4, 30
Heap    GATE-CS-2015 (Set 1)    
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   4
After insertion of 35, we get
         40
       /    \
     30      20
    / \       / \
   10  15  16   17
  / \  /
 8   4 35  
After 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 max-heap is
A
4
B
5
C
2
D
3
Heap    GATE-CS-2015 (Set 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 i-th node. Assume that the heap is implemented in an array and i refers to the i-th 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 re-fix the heap efficiently after the removal of the element?
A
O(1)
B
O(d) but not O(1)
C
O(2d) but not O(d)
D
O(d2d) but not O(2d)
Heap    GATE-CS-2016 (Set 1)    
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 max-heap).  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/binary-heap/   This solution is contributed by Pranjul Ahuja.
Question 19
A complete binary min-heap 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 Fill-in-the-Blanks question]
A
6
B
7
C
8
D
9
Heap    GATE-CS-2016 (Set 2)    
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?
A
{23, 17, 14, 6, 13, 10, 1, 12, 7, 5}
B
{23, 17, 14, 6, 13, 10, 1, 5, 7, 12}
C
{23, 17, 14, 7, 13, 10, 1, 5, 6, 12}
D
{23, 17, 14, 7, 13, 10, 1, 12, 5, 7}
Heap    GATE IT 2006    
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. heap 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 max-heap 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.

GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.