GATE 2013
80 questions
MCQ 801-mark 38 · 2-mark 42
Questions 26–30 of 80
Consider the following operation along with Enqueue and Dequeue operations on queues, where k is a global parameter.
MultiDequeue(Q){
m = k
while (Q is not empty) and (m > 0) {
Dequeue(Q)
m = m - 1
}
} What is the worst case time complexity of a sequence of n queue operations on an initially empty queue?Answer: (A)
Every element is enqueued one time and can be dequeued at most one time. So over n operations, the number of dequeues (the ones inside MultiDequeue too) can never be more than the number of enqueues.
Total work (amortised) =
Answer:
Total work (amortised) =
Answer:
The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?
Answer: (D)
Preorder is root, left, right, so the first key 30 is the root. In a BST the keys smaller than 30 form the left sub-tree and the keys larger than 30 form the right sub-tree, and they keep their preorder order.
Left sub-tree: 20, 10, 15, 25, 23. Right sub-tree: 39, 35, 42.
- Left: the root is 20 (first). Smaller keys 10, 15 go left of 20, larger keys 25, 23 go right of 20. So 10 and 25 are the left and right children of 20. Then 15 is larger than 10, so it is the right child of 10, and 23 is smaller than 25, so it is the left child of 25.
- Right: the root is 39. Its left child is 35 and its right child is 42.
Postorder (left, right, root): 15, 10, 23, 25, 20, then 35, 42, 39, then 30.
Answer: (D)
Left sub-tree: 20, 10, 15, 25, 23. Right sub-tree: 39, 35, 42.
- Left: the root is 20 (first). Smaller keys 10, 15 go left of 20, larger keys 25, 23 go right of 20. So 10 and 25 are the left and right children of 20. Then 15 is larger than 10, so it is the right child of 10, and 23 is smaller than 25, so it is the left child of 25.
- Right: the root is 39. Its left child is 35 and its right child is 42.
Postorder (left, right, root): 15, 10, 23, 25, 20, then 35, 42, 39, then 30.
Answer: (D)
Which one of the following is the tightest upper bound that represents the time complexity of inserting an object into a binary search tree of n nodes?
Answer: (C)
A BST can be skewed, with height n 1. So an insertion may have to pass through all n nodes.
Answer: O(n)
Answer: O(n)
The number of elements that can be sorted in time using heap sort is
Answer: (C)
Sorting k elements with heap sort takes . Now put .
This gives , and here .
Answer:
This gives , and here .
Answer:
Which one of the following is the tightest upper bound that represents the number of swaps required to sort n numbers using selection sort?
Answer: (B)
In selection sort, each pass makes at most one swap. So the total number of swaps is at most n 1.
Answer: O(n)
Answer: O(n)