KIET Group of Institutions
Department of Computer Science & Information Technology
Design and Analysis of Algorithm
Q1. Consider the following three functions. (GATE 2021 SET1)
f1 = 10n
f2 = nlogn
f3 = n√n
Which one of the following options arranges the functions in the increasing
order of asymptotic growth rate?
(A) f3,f2,f1
(B) f2,f1,f3
(C) f1,f2,f3
(D) f2,f3,f1
Answer: (D)
Explanation: On comparing power of these given functions :
f1 has n in power.
f2 has logn in power.
f3 has √n in power.
Hence, f2, f3, f1 is in increasing order.
Note that you can take the log of each function then compare.
Q2. Consider the following array. (GATE 2021 SET1)
Which algorithm out of the following options uses the least number of
comparisons (among the array elements) to sort the above array in ascending
order?
(A) Selection sort
(B) Mergesort
(C) Insertion sort
(D) Quicksort using the last element as pivot
Answer: (C)
Explanation: Since, given array is almost sorted in ascending order,
so Insertion sort will give its best case with time complexity of order O(n).
Q3. A binary search tree T contains n distinct elements. What is the time
complexity of picking an element in T that is smaller than the maximum element
in T? (GATE 2021 SET1)
(A) Θ(nlogn)
(B) Θ(n)
(C) Θ(logn)
(D) Θ(1)
Answer: (D)
Explanation: Pick any two element from the root, and return minimum of these
two.
So, time is Θ(1).
Q4. Consider the following recurrence relation. (GATE 2021 SET1)
Which one of the following options is correct?
(A) T(n) = Θ(n5/2)
(B) T(n) = Θ(nlogn)
(C) T(n) = Θ(n)
(D) T(n) = Θ((logn)5/2)
Answer: (C)
Explanation: Given, recurrence relation can be written as,
T(n) = T(n/2) + T(2n/5) + 7n
T(n) = T((5/2)n/5) + T(2n/5) + 7n
Since, sum of numerator (5/2+2 = 4.5) is less than denominator (5), so time
complexity would be function itself.
Hence, T(n) = Θ(7n) = Θ(n)
Q5. What is the worst-case number of arithmetic operations performed by
recursive binary search on a sorted array of size n? (GATE 2021 SET2)
(A) Θ(√n)
(B) Θ(log2(n))
(C) Θ(n2)
(D) Θ(n)
Answer: (B)
Explanation: Arithmetic operations performed by binary search on sorted data
items means computation of mid element required arithmetic operation. So it
will be computed log(n) time and Hence option (C) will be correct.
Q6. For constants a≥1 and b>1, consider the following recurrence defined on
the non-negative integers:
T(n) = a⋅T(n/b) + f(n)
Which one of the following options is correct about the recurrence T(n)? (GATE
2021 SET2)
(A) A
(B) B
(C) C
(D) D
Answer: (C)
Explanation: It is one of the cases of Master’s theorem.
Q. 7 For parameters a and b, both of which are ω(1), T(n)=T(n1/a)+1, and
T(b)=1. Then T(n) is (GATE 2020)
(A) Θ(logalogbn)
(B) Θ(logabn)
(C) Θ(logblogan)
(D) Θ(log2log2n)
Answer: (A)
Explanation: Given,
T(n) = T(n1/a)+1,
T(b) = 1
Now, using iterative method,
= T(n)
= [T(n1/a )+1] + 1
2
= [T(n1/a )+1] + 2
3
= [T(n1/a )+1] + 3
4
.
.
.
= [T(n1/a )+1] + (k-1)
k
= T(n1/a ) + k
k
Let,
→ n1/a = b
k
→ log(n1/a ) = log(b)
k
→ ak = log(n) / log (b) = log b(n)
→ k = logalogb(n)
Therefore,
= T(n1/a ) + k
k
= T(b) + logalogb(n)
= 1 + logalogb(n)
= Θ(logalogb(n))
Option (A) is correct.
Q8. The preorder traversal of a binary search tree is 15, 10, 12, 11, 20, 18, 16,
19. Which one of the following is the postorder traversal of the tree ? (GATE
2020)
(A) 10, 11, 12, 15, 16, 18, 19, 20
(B) 11, 12, 10, 16, 19, 18, 20, 15
(C) 20, 19, 18, 16, 15, 12, 11, 10
(D) 19, 16, 18, 20, 11, 12, 10, 15
Answer: (B)
Explanation: Please refer – Find postorder traversal of BST from preorder
traversal
// C++ program for finding postorder
// traversal of BST from preorder traversal
#include <bits/stdc++.h>
using namespace std;
// Function to find postorder traversal from
// preorder traversal.
void findPostOrderUtil(int pre[], int n, int minval,
int maxval, int& preIndex)
// If entire preorder array is traversed then
// return as no more element is left to be
// added to post order array.
if (preIndex == n)
return;
// If array element does not lie in range specified,
// then it is not part of current subtree.
if (pre[preIndex] < minval || pre[preIndex] > maxval) {
return;
// Store current value, to be printed later, after
// printing left and right subtrees. Increment
// preIndex to find left and right subtrees,
// and pass this updated value to recursive calls.
int val = pre[preIndex];
preIndex++;
// All elements with value between minval and val
// lie in left subtree.
findPostOrderUtil(pre, n, minval, val, preIndex);
// All elements with value between val and maxval
// lie in right subtree.
findPostOrderUtil(pre, n, val, maxval, preIndex);
cout << val << " ";
// Function to find postorder traversal.
void findPostOrder(int pre[], int n)
// To store index of element to be
// traversed next in preorder array.
// This is passed by reference to
// utility function.
int preIndex = 0;
findPostOrderUtil(pre, n, INT_MIN, INT_MAX, preIndex);
// Driver code
int main()
{
int pre[] = { 15, 10, 12, 11, 20, 18, 16, 19 };
int n = sizeof(pre) / sizeof(pre[0]);
// Calling function
findPostOrder(pre, n);
return 0;
Q9. What is the worst case time complexity of inserting n elements into an
empty linked list, if the linked list needs to be maintained in sorted order ?
(GATE 2020)
(A) Θ(n)
(B) Θ(n log n)
(C) Θ(n2)
(D) Θ(1)
Answer: (C)
Explanation: This question is ambiguous: “needs to be maintained in sorted
order”, there are two possible cases:
1. Needs to be maintained in sorted order on each step (after each insertion).
When we are inserting an element in to empty linked list and to perform
sorted order list of every element will take O(n2).
Each Insertion into a sorted linked list will take θ(n) and hence the total cost
for n operations is θ(n2).
2. Needs to be maintained in sorted order on final step (only after all insertion).
When we are inserting all elements into an empty linked list and to perform a
sorted list (using merge sort) after inserting all elements will take O(n log n)
time.
Official answer by GATE is Θ(n2).
Q10. In a balanced binary search tree with n elements, what is the worst case
time complexity of reporting all elements in range [a,b] ? Assume that the
number of reported elements is k. (GATE 2020)
(A) Θ(log n)
(B) Θ(log(n)+k)
(C) Θ(k log n)
(D) Θ(n log k)
Answer: (B)
Explanation:
1. Time complexity to check if element ‘a’ in given balanced binary search tree
= O(log n)
2. Time complexity to check if element ‘a’ in given balanced binary search tree
= O(log n)
3. Now, time complexity to traverse all element in range [a, b], those elements
will be inoder sorted = θ(k)
Therefore, total time complexity will be,
= Θ(log(n)) + Θ(log(n)) + Θ(k)
= Θ(2(log(n))+k)
= Θ(log(n)+k)
Option (B) is correct.