GATE 2009
66 questions
MCQ 661-mark 24 · 2-mark 42
Questions 31–35 of 66
The following key values are inserted into a B+ tree in which order of the internal nodes is 3, and that of the leaf nodes is 2, in the sequence given below. The order of internal nodes is the maximum number of tree pointers in each node, and the order of leaf nodes is the maximum number of data items that can be stored in it. The B+ - tree is initially empty.
10, 3, 6, 8, 4, 2, 1
The maximum number of times leaf nodes would get split up as a result of these insertions is
10, 3, 6, 8, 4, 2, 1
The maximum number of times leaf nodes would get split up as a result of these insertions is
Answer: (C)
A leaf holds at most 2 keys. An internal node has at most 3 pointers, so at most 2 keys. A node that gets one key too many splits. The question asks for the maximum number of leaf splits, so split a 3-key leaf with the first 2 keys in the left leaf and the last key in the right leaf. The key copied up is the largest key of the left leaf, and keys up to it go left.
Insert 10, 3: leaf [3, 10].
Insert 6: [3, 6, 10] overflows. Leaf split 1: [3, 6] and [10]. Root [6].
Insert 8: goes right, [8, 10].
Insert 4: goes left, [3, 4, 6] overflows. Leaf split 2: [3, 4] and [6]. Root [4, 6].
Insert 2: goes to [3, 4], giving [2, 3, 4]. Leaf split 3: [2, 3] and [4]. The key 3 goes up, so the root [3, 4, 6] has too many keys and splits (an internal split, not a leaf split). The key 4 moves up to a new root [4], over the internal nodes [3] and [6].
Insert 1: goes to [2, 3], giving [1, 2, 3]. Leaf split 4: [1, 2] and [3]. The key 2 joins the internal node [3], which becomes [2, 3] and has room.
So the leaves split 4 times. Splitting the other way ([3, 6, 10] into [3] and [6, 10]) gives fewer: any mix of the two ways gives 3 or 4. The maximum is 4.
Answer: (C)
Insert 10, 3: leaf [3, 10].
Insert 6: [3, 6, 10] overflows. Leaf split 1: [3, 6] and [10]. Root [6].
Insert 8: goes right, [8, 10].
Insert 4: goes left, [3, 4, 6] overflows. Leaf split 2: [3, 4] and [6]. Root [4, 6].
Insert 2: goes to [3, 4], giving [2, 3, 4]. Leaf split 3: [2, 3] and [4]. The key 3 goes up, so the root [3, 4, 6] has too many keys and splits (an internal split, not a leaf split). The key 4 moves up to a new root [4], over the internal nodes [3] and [6].
Insert 1: goes to [2, 3], giving [1, 2, 3]. Leaf split 4: [1, 2] and [3]. The key 2 joins the internal node [3], which becomes [2, 3] and has room.
So the leaves split 4 times. Splitting the other way ([3, 6, 10] into [3] and [6, 10]) gives fewer: any mix of the two ways gives 3 or 4. The maximum is 4.
Answer: (C)
The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially empty hash table of length 10 using open addressing with hash function h(k) = k mod 10 and linear probing. What is the resultant hash table?
Answer: (C)
Linear probing: if slot is full, try the next slot, then the next, wrapping around at the end of the table.
12 goes to slot 2. 18 goes to slot 8. 13 goes to slot 3.
2: slot 2 is full, so try 3 (full), then 4. It goes to slot 4.
3: slots 3 and 4 are full, so it goes to slot 5.
23: slots 3, 4, 5 are full, so it goes to slot 6.
5: slots 5 and 6 are full, so it goes to slot 7.
15: slots 5, 6, 7 are full and slot 8 holds 18, so it goes to slot 9.
Final table: slot 2 = 12, 3 = 13, 4 = 2, 5 = 3, 6 = 23, 7 = 5, 8 = 18, 9 = 15. This is table C. Tables A and B hold only 4 of the 8 keys, and D puts several keys in one slot, which is chaining, not open addressing.
Answer: (C)
12 goes to slot 2. 18 goes to slot 8. 13 goes to slot 3.
2: slot 2 is full, so try 3 (full), then 4. It goes to slot 4.
3: slots 3 and 4 are full, so it goes to slot 5.
23: slots 3, 4, 5 are full, so it goes to slot 6.
5: slots 5 and 6 are full, so it goes to slot 7.
15: slots 5, 6, 7 are full and slot 8 holds 18, so it goes to slot 9.
Final table: slot 2 = 12, 3 = 13, 4 = 2, 5 = 3, 6 = 23, 7 = 5, 8 = 18, 9 = 15. This is table C. Tables A and B hold only 4 of the 8 keys, and D puts several keys in one slot, which is chaining, not open addressing.
Answer: (C)
In quick sort, for sorting n elements, the (n/4)th smallest element is selected as pivot using an O(n) time algorithm. What is the worst case time complexity of the quick sort?
Answer: (B)
The pivot is the (n/4)-th smallest element, so it splits the array into n/4 1 elements on the left and 3n/4 on the right. Finding the pivot costs and partitioning costs . The recurrence is
.
Draw the recursion tree. The sizes at any level add up to at most n, so each level costs at most . The longest branch always takes the 3n/4 part, so its depth is , which is . So .
The shortest branch takes the n/4 part and has depth about . Every level above that depth is full and costs , so as well.
So the worst case is . It is not because the split is never worse than n/4 : 3n/4.
Answer: (B)
.
Draw the recursion tree. The sizes at any level add up to at most n, so each level costs at most . The longest branch always takes the 3n/4 part, so its depth is , which is . So .
The shortest branch takes the n/4 part and has depth about . Every level above that depth is full and costs , so as well.
So the worst case is . It is not because the split is never worse than n/4 : 3n/4.
Answer: (B)
What is the number of swaps required to sort n elements using selection sort, in the worst case?
Answer: (A)
Selection sort runs one pass for each position . In a pass it first finds the minimum of the unsorted part using only comparisons, then makes a single swap to put that minimum in position i.
So each pass does at most 1 swap, and there are passes. The number of swaps is at most in the worst case, and the worst case does reach about n swaps. So the number of swaps is . The part of selection sort comes from the comparisons, not from the swaps.
Answer: (A)
So each pass does at most 1 swap, and there are passes. The number of swaps is at most in the worst case, and the worst case does reach about n swaps. So the number of swaps is . The part of selection sort comes from the comparisons, not from the swaps.
Answer: (A)
The running time of an algorithm is represented by the following recurrence relation:
Which one of the following represents the time complexity of the algorithm?
Which one of the following represents the time complexity of the algorithm?
Answer: (A)
Compare the given recurrence with the Master Theorem form
This gives
So is true for any . This is the third case of the Master theorem. The Master theorem also needs one more check in case 3, the regularity condition. This check makes sure still dominates as the recurrence goes deeper. The condition is for some and all large enough (we write here because is already used in
Here we have
So any satisfies the regularity condition, and case 3 of the Master theorem holds. Now we apply case and get
The answer is A.