GATE 2006
191 questions
MCQ 1911-mark 191
Questions 106–110 of 191
Which of the following sequences of array elements forms a heap?
Answer: (C)
In a max heap stored in an array with indices starting at 1, the node at index has children at and . Each parent must be at least as large as its children. With 10 elements, only indices 1 to 5 have children, so check those.
(A) a[4] = 6 has child a[8] = 12, and 12 is larger. Not a heap.
(B) a[4] = 6 has child a[9] = 7, and 7 is larger. Not a heap.
(D) a[4] = 7 has child a[8] = 12, and 12 is larger. Not a heap.
(C) 23 ≥ 17, 14; 17 ≥ 7, 13; 14 ≥ 10, 1; 7 ≥ 5, 6; 13 ≥ 12. Every parent is at least as large as its children, so it is a heap.
Answer: (C)
(A) a[4] = 6 has child a[8] = 12, and 12 is larger. Not a heap.
(B) a[4] = 6 has child a[9] = 7, and 7 is larger. Not a heap.
(D) a[4] = 7 has child a[8] = 12, and 12 is larger. Not a heap.
(C) 23 ≥ 17, 14; 17 ≥ 7, 13; 14 ≥ 10, 1; 7 ≥ 5, 6; 13 ≥ 12. Every parent is at least as large as its children, so it is a heap.
Answer: (C)
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.
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 (the array 9, 5, 6, 8, 3, 1), Which one of the following is the sequence of items in the array representing the resultant heap?
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 (the array 9, 5, 6, 8, 3, 1), Which one of the following is the sequence of items in the array representing the resultant heap?
Answer: (A)
The 3-ary heap from the previous part is in to . The children of are , , , so the parent of is .
Insert 7 at : parent = 5 < 7, so swap. Then the parent = 9 > 7, so stop. Array: 9, 7, 6, 8, 3, 1, 5.
Insert 2 at : parent = 6 > 2, so no swap. Array: 9, 7, 6, 8, 3, 1, 5, 2.
Insert 10 at : parent = 6 < 10, so swap. Then the parent = 9 < 10, so swap again. Array: 10, 7, 9, 8, 3, 1, 5, 2, 6.
Insert 4 at : parent = 9 > 4, so no swap.
Final array: 10, 7, 9, 8, 3, 1, 5, 2, 6, 4.
Answer: (A)
Insert 7 at : parent = 5 < 7, so swap. Then the parent = 9 > 7, so stop. Array: 9, 7, 6, 8, 3, 1, 5.
Insert 2 at : parent = 6 > 2, so no swap. Array: 9, 7, 6, 8, 3, 1, 5, 2.
Insert 10 at : parent = 6 < 10, so swap. Then the parent = 9 < 10, so swap again. Array: 10, 7, 9, 8, 3, 1, 5, 2, 6.
Insert 4 at : parent = 9 > 4, so no swap.
Final array: 10, 7, 9, 8, 3, 1, 5, 2, 6, 4.
Answer: (A)
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?
Which one of the following is a valid sequence of elements in an array representing 3-ary max heap?
Answer: (D)
With the array starting at index 0 and 3 children per node, the children of a[i] are a[3i+1], a[3i+2] and a[3i+3]. So a[0] has children a[1], a[2], a[3], and a[1] has children a[4], a[5], a[6]. Each array has 6 elements, so only a[0] and a[1] have children (a[1] has only a[4] and a[5]). In a max heap every parent must be at least as large as its children.
(A) The root is 1, smaller than its child 3. Not a heap.
(B) a[1] = 6 has child a[4] = 8, which is larger. Not a heap.
(C) a[1] = 3 has child a[4] = 5, which is larger. Not a heap.
(D) The root 9 is at least 5, 6 and 8. And a[1] = 5 is at least its children a[4] = 3 and a[5] = 1. It is a valid 3-ary max heap.
Answer: (D)
(A) The root is 1, smaller than its child 3. Not a heap.
(B) a[1] = 6 has child a[4] = 8, which is larger. Not a heap.
(C) a[1] = 3 has child a[4] = 5, which is larger. Not a heap.
(D) The root 9 is at least 5, 6 and 8. And a[1] = 5 is at least its children a[4] = 3 and a[5] = 1. It is a valid 3-ary max heap.
Answer: (D)
In a binary max heap containing n numbers, the smallest element can be found in time
Answer: (A)
In a max heap every parent is at least as large as its children, so the smallest element cannot be an internal node (it would have a smaller child, or equal). It sits at a leaf. A heap with nodes has leaves.
There is no order among the leaves. Any one of them can be the minimum, and a leaf we skip could hold a value smaller than all the others without breaking the heap property. So all leaves must be checked, which needs time.
Scanning the leaves (or the whole array) takes , so the time is .
Answer: (A)
There is no order among the leaves. Any one of them can be the minimum, and a leaf we skip could hold a value smaller than all the others without breaking the heap property. So all leaves must be checked, which needs time.
Scanning the leaves (or the whole array) takes , so the time is .
Answer: (A)
The median of n elements can be found in O(n) time. Which one of the following
is correct about the complexity of quick sort, in which median is selected as pivot?
Answer: (B)
If we pick the median as the pivot, the array always splits into two equal halves. This is the best case of quick sort.
Recurrence:
Correct Answer:
Recurrence:
Correct Answer: