0% found this document useful (0 votes)
13 views

Advanced DSA Sheet 2023

Uploaded by

oneplus61969
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Advanced DSA Sheet 2023

Uploaded by

oneplus61969
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

GATE Computer Science & IT

ADVANCED
DATA STRUCTURES
AND
ALGORITHMS

Practice Questions
Booklet

Analysis for GATE Papers DATA STRUCTURE AND ALGORITHM


Years Marks
2015 15
2016 10
2017 06
2018 07
2019 10
2020 12
2021 11
2022 12
DATA STRUCTURE AND ALGORITHM SYLLABUS
 Programming and Data structure: Recursion, Stack, Queue, Linked –list, Trees
and graphs, Heap, priority queue.

 Searching and sorting technique: Searching (Linear Search, Binary Search),


Sortingtechniques (Bubble sort, Selection sort, Insertion sort, Heap sort, Counting
sort, Radix sort).

 Algorithm Analysis: Asymptotic Notation (Big „Oh‟, Theta, Big Omega, small „Oh‟,
Small Omega), Time and space complexity of an algorithm, Recurrence relation:
Types of recurrence relation and their solutions (Master‟s method, Substitution
Method and Tree method).

 Program Design strategy: Greedy problems (Fractional knapsack problem, Job


scheduling problem, Huffman coding and optimal file merge pattern.

 DP Approach: Matrix chain multiplication problem, longest common subsequence,


subset sum problem.

 Divide and conquer strategy: Quick sort and merge sort.

Data structure and Algorithms GATE REFERENCE BOOK

 Introduction to Algorithms BY THOMS H. CORMAN & CHARLES E.LELSRSON &


RONALD L. RIVEST & CLIFFORD STEIN

 Fundamentals of Computer Algorithms By Ellis Horowitz , SartajSahni


Q1. Given an n×m matrix arr, you want to create another matrix newArr with the
elements in arr shifted by a shift amount. For example, given the matrix arr on
the left below, and a shift amount of 3, the matrix newArr on the right is
created.

The elements in arr are shifted 3 positions in row-major order, and if an


element is shifted out of the matrix, it appears at the start of the matrix (see
elements 10, 11, 12). One way to do is to “linearise” the element positions in
arr, and map their respective new positions in newArr. For example, the second
element in arr now appears in the fifth position in newArr. The partial code is
shown below:
int[][] newArr = new int[arr.length][arr[0].length];
int index, numRows = arr.length, numCols = arr[0].length;
for (int r = 0; r <numRows; r++) {
for (int c = 0; c <numCols; c++) {
index = ______________;
newArr[index/numCols] [index % numCols] = arr[r][c];
}
}
Complete the missing part, where shiftAmt is the shift amount:
(a) (r*numCols + c + shiftAmt);
(b) (r*numCols + c + shiftAmt) % numRows;
(c) (r*numCols + c + shiftAmt) % numCols;
(d) (r*numCols + c +shiftAmt) % (numRows*numCols);

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 1


Q2. Which algorithm does the following doSort () method actually implement?
/* maxVal () returns the index of the maximum element in the part of the
array from a[startIndex] to a[endIndex], inclusive. */
/*Swap(a, b): It swaps the values of a and b
int maxVal(int[] a, int startIndex, int endIndex) {
________// Code not shown.
}
void doSort (int [] a, int n)
{

for (int i = n-1; i >= 1; i--)


{
int maxIndex = maxVal (a, 0, i);
Swap(a[i], a[maxIndex]);
}
}
(a) Selection sort (b) Insertion sort
(c) Bubble sort (d) None of these (Some other sort)

For next two questions: Consider the following C code


void sort (int data [], int low, int high)
{
for (int outer = low; outer < high; outer++) {
for (int inner = low; inner < high; inner++) {
if (data[inner] > data [inner + 1]) {
int temp = data [inner];
data [inner] = data [inner + 1];
data [inner + 1] = temp;
}
}
for (int index=low; index<=high; index++)
printf (“% d”, a);
printf (“\n”);
}
}

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 2


Q3. What will be the first line printed by this function when we call “sort(data,0,9)”
with the array “int data [10] = {8, 2, 0, 6, 3, 5, 7, 1, 9, 4};”?
(a) 2 0 6 3 5 7 1 8 4 9
(b) 0 1 2 3 4 5 6 7 8 9
(c) 0 2 8 6 3 5 7 1 9 4
(d) 8 2 0 6 3 5 7 1 4 9
Q4. How many numbers will be printed to the screen if we call “sort(data,0,9)” with
the array “int data [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};”?
(a) 10 (b) 100 (c) 9 (d) 90

Q5. Suppose we choose the median of five items as the pivot in quicksort. If we
have an N element array, then we find the median of the elements located at the
following positions: left (= 0), right (= N – 1), center (the average of left and right,
rounded down), left Of Center (the average of left and center, rounded down),
and right Of Center (the average of right and center, rounded down). The
median of these elements is the pivot.
What is the worst-case running time (in tightest upper bound) of this version of
quick-sort?
(a)O(N) (b)O (N log N) (c) O(N2) (d)O (N2log N)
Q6. Kruskal‟s minimum spanning tree algorithm uses a heap to quickly find the
lowest cost edge not already chosen. What would be the running time of the
algorithm if instead of using a heap, the algorithm used a simple unsorted array
to store the edges? [Assume G=(V, E) is the graph]
(a) O (|E|) (b) O (|E|2)
(c) O (|E|. log |V|) (d) O (V log V)
Q7. Consider the following modification to Mergsort, called 3-way Mergsort.
Instead of dividing the input array into two sub-arrays, 3-way Mergsort divides
the array into three sub-arrays (as equally-sized as possible), sorts them
recursively, and then calls a 3-way merge function, which combines three
sorted arrays into one sorted array. What is the worst-case number of
comparisons 3-way merge makes if given as input three arrays, each of size M?
(a)5M - 3 (b)5M – 2 (c)6M – 3 (d) 3M – 2

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 3


For next two questions consider the following C code
int Search (int X, int Data[], int Min, int Max)
{
int Mid = (Min + Max) / 2;
if (Max < Min) return -1;
else if (Data [Mid] == X) return Mid;
else if (Data [Mid] < X) return (Search (X, Data, Mid+1, Max));
else if (Data [Mid] > X) return (Search (X, Data, Min, Mid-1));
}
int main ()
{
int Data [10] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
Search(____________);
return 0;
}
Q8. What would happen if you called “Search (-42, Data, 0, 9)” with the Data array
above?
(a) The function would return -1.
(b) The function would return 0.
(c) The function would return 9.
(d) The recursion would go on forever.
Q9. What would happen if you called “Search (X, Data, 0, 9)” with a Data array that
was in random order (e.g. {3,1,4,1,5,9,3,6,5,2} ) and X between [1..9]?
(a) The function would always return -1.
(b) The function would always return 0.
(c) The recursion would go on forever.
(d) You might get lucky and find the value.
Q10. Suppose you have algorithms with the running time 2n. Assume these are the
exact number of operations performed as a function of the input size n.
Suppose you have a computer that can perform 1010 operations per second, and
you need to compute a result in at most an hour of computation. What is
largest input size n for which you would be able to get the result within an
hour? _________

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 4


Q11. Consider the following piece of code that manipulates a linked list:
p = head;
while(p->val != 6)
p = p->next;
q = p->next;
p->next = p->next->next;
q->next = q->next->next;
p->next->next = q;
v = p->val;
p->val = p->next->val;
p->next->val = v;
while(q != NULL) {
q->val = q->val + 1;
q = q->next;
}
Before executing this code, head points to the beginning of the following linked
list: head 17695NULL
What will be the result of linked list after execution of the above code fragment?
(a) head 175610NULL (b)head 17569NULL
(c) head 17695NULL (d)head 176510NULL

Q12. Match the expressions on the left side with the expression on the right side that
denote the same order of growth.
𝑛2 i. log 2 𝑛 log 2 𝑛
1. + 𝑛 log 2 𝑛
2
𝑛 1
2. 𝑟=1 𝑟 ii. (𝑛2 + log 2 𝑛)
𝑛

3. 100 4 +
1
iii. 4𝑛 4log 16 𝑛 + 3log 3 𝑛
𝑛

4.
log 2 𝑛
2𝑟 iv. 𝑛1.5
𝑟=1

5. 𝑛log 2 log 2 𝑛 50 𝑖
v. 𝑖=1 2

(a) 1 – ii, 2 –iv, 3 – v, 4 – i, 5 – iii


(b) 1 – iii, 2 – iv, 3 – v, 4 – ii, 5 – i
(c) 1 – iii, 2 – ii, 3 – v, 4 – iv, 5 – i
(d) 1 – ii, 2 – iv, 3 – v, 4 – iii, 5 – i

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 5


Q13. Suppose we need to implement a stack for some application. We want to keep
the total space usage as low as possible. We know in advance that N elements
will be pushed on the stack, and that all N elements are pushed before any are
popped. What is the most suitable stack representation?
(a) A linked list of elements in the order they were pushed
(b) An array of size N holding elements in the order they were pushed
(c) An array of size N holding elements according to the heap layout
(d) A Huffman tree.

Q14. We are given a file consisting of several million bytes. Out of the 256 possible
byte values, only 10 are used, namely the alphabet „a‟,‟b‟, „c‟ ……‟j‟. Apart from
this restriction, the file appears completely random: all the digits are equally
probable, and there are no repeating patterns beyond what can be expected in a
random string. We encode the file using a compression program based on
Huffman coding. Approximately how large can we expect the encoded output to
be in relation to the original file size?
(a) 43% (b) 50% (c) 100% (d) 110%

Q15. A looped tree is a weighted, directed graph built from a binary tree by adding an
edge from every leaf back to the root. Every edge has a non-negative weight

In this example, the shortest path from the leftmost leaf to the rightmost leaf is
27. Given a looped graph and two vertices s and t, we wish to find the shortest
path from s to t. Clearly, Dijkstra‟s algorithm solves the problem. What is the
running time in terms of the number of nodes N?
[Select answer in tightest upper bound]
(a)O (N2) (b) O (N2 log N) (c) O (N log N) (d)O (N)

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 6


Q16. [MSQ]
Arrange the following functions in ascending order of their growth rate:
log 2 𝑛 𝑛
𝑔1 𝑛 = 2 , 𝑔2 𝑛 = 22 , 𝑔3 𝑛 = 𝑛 log 2 𝑛 3 , 𝑔4 𝑛 = 𝑛4/3 , 𝑔5 𝑛 = 𝑛log 2 𝑛 ,
1 3
𝑔6 𝑛 = 2𝑛 , 𝑔7 𝑛 = 𝑛log 2 𝑛 , 𝑔8 𝑛 = 𝑛!, 𝑔9 𝑛 = 𝑛1+1/ log 2 log 2 𝑛 ,

𝑔10 𝑛 = log 2 𝑛 2 , 𝑔11 𝑛 = log 2 𝑛 log 2 𝑛


, 𝑔12 𝑛 = 𝑛𝑛 .
Which of the following statement is/are true?
(a) 𝑔7 < 𝑔1 < 𝑔9 < 𝑔10 < 𝑔11 < 𝑔3 < 𝑔4 < 𝑔5
(b) 𝑔7 < 𝑔1 < 𝑔10 < 𝑔9 < 𝑔3 < 𝑔4 < 𝑔11 < 𝑔5
(c) 𝑔3 < 𝑔4 < 𝑔11 < 𝑔5 < 𝑔6 < 𝑔8 < 𝑔12 < 𝑔2
(d) 𝑔7 < 𝑔10 < 𝑔3 < 𝑔4 < 𝑔5 < 𝑔8 < 𝑔12 < 𝑔2

Q17. [MSQ]
Which of the following is/are true?
(a) Let T denote a binary search tree. If node a in T has two children, then its
successor has no left child and its predecessor has no right child.
(b) Let T denote a binary search tree. If the keys in T are distinct and x is a leaf
node and y is x‟s parent, then y „s key is either the smallest key in T larger than
x‟s key, or the largest key in T smaller than x‟s key.
(c) Let G be a graph where each edge has 2 weight functions, w and v where
w(e) = v(e) +1 and v(e)>0 for each edge e  G. If tree T is a minimum spanning
tree using weight function v then it is also a minimum spanning tree using
weight function w.
(d) Let G be a graph where each edge has 2 weight functions, w and v where
w(e) = v(e) +1 and v(e)>0 for each edge e  G. If tree T is a shortest path tree
using weight function v then it is also a shortest path tree using weight function
w.

Q18. You have an array with 32,000,000 distinct elements in random order. You
have to search the array a number of times to determine if a given element is
present or not. What number of searches will make it worthwhile (result in
overall less work) to sort the array before doing the searches? ______________

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 7


Q19. Consider the following time data for a method that uses an unknown sorting
algorithm:
• time to sort array with 1,000,000 elements all equal to the same value: 1 sec
• time to sort array with 2,000,000 elements all equal to the same value: 2 sec
• time to sort an array with 100,000 distinct elements in random order: 5
seconds
• time to sort an array with 200,000 distinct elements in random order: 20
seconds
Which of the following algorithms can exhibit this behaviour?
(a) Insertion Sort
(b) Selection Sort
(c) Merge Sort
(d) Quick Sort

Q20. The graph below has negative edge weights. What is the best way of solving the
single source shortest path problem in the presence of negative edge weights?

(a) add 5 to each edge and then proceed with Dijkstra‟s algorithm.
(b) add 5 to each edge and then proceed with the Floyd-Warshall algorithm.
(c) keep a queue of nodes to be examined and anytime the distance to a node
change, put it back on the queue
(d) Normal shortest path algorithms will solve the problem because there are no
negative cycles.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 8


Information for next three questions
Different applications impose different requirements on the running time of the
methods supported by a data structure. Next three questions below give examples of
such requirements. For each one, list which data structure can meet the requirements.
Assume you want to build a dictionary to store (key, value). Specifically, you would
like to implement a data structure that supports these methods:
• insert(key, value) - Add a pair to the dictionary.
• delete(key) - Remove the key.
• lookup(key) - Determine if the key is in the dictionary.
• print() - Enumerate all elements in sorted order by keys of the dictionary.
• findMin() - Return the minimum element.
• removeMin() - Take the minimum element out.
Q21. Which data-structure you prefer to implement dictionary if the following
function should be supported in given time bound?
findMin(): O(1)
removeMin(): O(log n)
insert(): O(log n)
print(): O(n log n)
[If a function is not listed, that means its time requirement is unbounded]
(a)Min-heap (b)AVL Tree (c) BST (d) Linked-list

Q22. Which data-structure you prefer to implement dictionary if the following


function should be supported in given time bound?
insert(): O(log n)
lookup(): O(log n)
delete(): O(log n)
[If a function is not listed, that means its time requirement is unbounded]
(a)Min-heap (b)AVL Tree (c) BST (d) Linked-list

Q23. Which data-structure you prefer to implement dictionary if the following


function should be supported in given time bound?
insert(): O(1)
lookup(): O(1)
delete(): O(1)
[If a function is not listed, that means its time requirement is unbounded]
(a)Min-heap (b)AVL Tree (c) BST (d)Hash Table

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 9


Q24. Consider the problem of computing the minimum of a set of 𝒏 distinct numbers.
We choose a permutation uniformly at random (i.e., each of the n! permutations
of 1, 2, 3 … 𝑛 is chosen with probability 1/𝑛! and we inspect the numbers in the
order given by this permutation. We maintain a variable MIN that holds the
minimum value seen so far. MIN is initialized to ∞and if we see a value smaller
than MIN during our inspection, then MIN is updated. For example, in the
inspection given by the following sequence, MIN is updated four times.
7, 9, 15, 5, 6, 2, 8, 11, 0, 4, 1, 16
What is the expected number of times MIN is updated?
(a)O(1)
𝑛
(b) 𝐻𝑛 = 𝑖=1 1/𝑖

(c) 𝑛
(d) n/2

Q25. Consider the following code fragment, where a[] is an array of size N
int min = N;
for (int i = 0; i < N; i++) {
selection_Sort (a, N); // A function which perform selection
sort
for (int j = 0; j < N; j++) {
for (int k = j+1; k < N; k++) {
if (a[j] <= 1.0) {
a[j] = a[j] * k;
} } } }
Suppose that the code fragment takes 30 seconds when N = 2000. What is the
running time (in seconds) for input size N = 16000? _________________

𝑛 𝑛
Q26. Let S1 = 𝑖=1 𝑖
2
and S2 = ( 𝑖=1 𝑖 ) ,
3 which one of the following statements is true?
(a) S1 = S2 for 1  n  30 only
(b) S1 = S2 for 1  n  100 only
(c) S1 = S2 for all n
(d) None of the above

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 10


Q27. Consider the undirected, weighted graph G:

Suppose Dijkstra‟s algorithm is used on G to find a shortest paths tree rooted at


vertex A. What will be the third, fifth, seventh, and ninth vertices to be removed
from the priority queue via a deleteMin operation?
(a) 3rd vertex: B, 5th vertex: C, 7th vertex: H, 9th vertex: G
(b) 3rd vertex: E, 5th vertex: C, 7th vertex: G, 9th vertex: H
(c) 3rd vertex: E, 5th vertex: F, 7th vertex: H, 9th vertex: G
(d) 3rd vertex: E, 5th vertex: C, 7th vertex: H, 9th vertex: G

Q28. The merge function employed by Mergsort allocates some auxiliary space each
time it is called. If we call Mergsort with an array of size n, how much extra
space does Mergsort allocate, overall?
(a) O(1) (b)O(n log n) (c) O(log n) (d)O(n)

Q29. What is the output from the following recursive method when called on the

linked list
void display_Numbers(struct Node *p) {
if( p == NULL )
return;
else {
display_Numbers(p->next);
display_Numbers(p->next);
printf(“%d ”, p->item);
}
}
(a) 7 7 9 7 7 2 7 7 9 7 7 9 7 7 9 2 5
(b) 7 7 9 7 7 9 2 7 7 9 7 7 9 7 7 9 2 5
(c) 7 7 9 9 7 7 2 7 7 9 9 7 7 7 7 9 2 5
(d) None of the above

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 11


Q30. We are given an array A that contains n distinct values. We are told that the
array A had once been sorted, but then it was rotated by some unknown
number of positions. E.g., The array below was rotated either 7 positions left or
10 positions right, but assume these rotation distances are unknown to us.

What is the time complexity of an efficient algorithm that returns the smallest
value in A?
(a)Θ 1 (b)Θ log 2 𝑛 (c)Θ 𝑛 (d)Θ 𝑛 log 2 𝑛
For next four questions the reference function g(n) = n2 applies to the leftmost Venn
diagram below. The reference function h(n) = n log n, applies to the rightmost Venn
diagram:

Q31. [MSQ]
If a function f1 (n)  V in the right Venn diagram, what is its most accurate
location in the left diagram?
(a) A (b) B (c) C (d) D
Q32. [MSQ]
If a function f2 (n)  W in the right Venn diagram, what is its most accurate
location in the left diagram?
(a) A (b) B (c) C (d) D
Q33. [MSQ]
If a function f3 (n)  D in the left Venn diagram, what is its most accurate
location in the right diagram?
(a) W (b) X (c) Y (d) Z
Q34. [MSQ]
If a function f4 (n)  E in the left Venn diagram, what is its most accurate
location in the right diagram?
(a) W (b) X (c) Y (d) Z

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 12


For next three questions suppose that you have two binary heaps, A and B, each
with exactly 2k − 1 elements (k > 0) and each represented as a binary tree. You can
combine them into a single binary heap by following these steps:
1. Remove the rightmost node from the bottom level of B. Call this node x and call the
modified binary heap B′.
2. Create a new binary tree rooted at x by making A the left child of x and B′ the right
child of x.
3. Establish the heap property for this tree by using the heapify algorithm that is
employed when the minimum element is deleted from a binary heap.
Q35. What is the worst-case complexity of this algorithm in terms of k?
[Give a tight O() upper bound]
(a)O(2k) (b)O(k) (c)O(k log k) (d) O (k2)
Q36. Given that A has (2k – 1) elements, what is the minimum size that B can have
without compromising the algorithm‟s correctness?
(a) 2k – 1 – 1 (b)2k – 1 (c) k (d)2k-1

Q37. Given that A has 2k − 1 elements, what is the maximum size that B can have
without compromising the algorithm‟s correctness?
(a) k – 1 (b)2k – 1 (c) k (d) 2k

Q38. Consider the following problem on a dictionary of n words, W1 . . . Wn, each


with exactly k characters. You can transform a word Wi into word Wj if they
differ in at most d ≤ k characters. (both d and k are specified as part of the
input, along with n and the words)
For example, if the dictionary is
W1 = „hit‟, W2 = „cog‟, W3 = „hot‟, W4 = „dot‟, W5 = „dog‟, W6 = „lot‟, W7 = „log‟,
and d = 1, one way to change „hit‟ to „cog‟ is:
‟hit‟ → „hot‟ → „dot‟ → „dog‟ → „cog‟.
We want to find the fewest number of steps to transform Wi to Wj. We are trying
to solve this problem with the help of some graph algorithm. To construct the
graph create one vertex vi for each word Wi. Place an edge from i to j if Wi can
be transformed into Wj by changing at most d characters. Which graph
algorithm returns the correct solution to this problem?
(a) Minimum weight spanning tree.
(b) Shortest path problem.
(c) DFS
(d)BFS

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 13


Q39. [MSQ]
Consider the random variable:
𝑥 𝑥2 𝑥3 𝑥4 𝑥5 𝑥6 𝑥7
X= 1
0.3 0.26 0.12 0.04 0.04 0.04 0.2
Which of the following can be valid ternary Huffman coding for X?
(a) 𝑥1 = 1, 𝑥2 = 0, 𝑥3 = 21, 𝑥4 = 200, 𝑥5 = 202, 𝑥6 = 201, 𝑥7 = 212
(b) 𝑥1 = 1, 𝑥2 = 0, 𝑥3 = 21, 𝑥4 = 200, 𝑥5 = 202, 𝑥6 = 201, 𝑥7 = 22
(c) 𝑥1 = 1, 𝑥2 = 0, 𝑥3 = 21, 𝑥4 = 200, 𝑥5 = 201, 𝑥6 = 202, 𝑥7 = 22
(d) 𝑥1 = 0, 𝑥2 = 1, 𝑥3 = 20, 𝑥4 = 22, 𝑥5 = 210, 𝑥6 = 221, 𝑥7 = 212
Q40. Consider the code given below:
void traverse( struct tree * p1)
{
if(p1 != NULL)
{
traverse(p1->left);
printf (“%d “, p1->data);
traverse(p1->right);
if(p1->data % 2 == 0) traverse(p1-
>left);
else traverse(p1->right);
}
}
Let T(n) be the total number of times the “Print” statement is going to be
executed when the Traversal algorithm is called on the root of a full heap of n
nodes (a full heap is one where the last level is full). Then what is the
recurrence relation for
T (n)?
𝑛
3𝑇 + 1; 𝑖𝑓 𝑛 > 1
(a) T (n) = 2
1; 𝑖𝑓 𝑛 = 1
𝑛
3𝑇 − 1 + 1; 𝑖𝑓 𝑛 > 1
(b) T (n) = 2
1; 𝑖𝑓 𝑛 = 1
𝑛−1
3𝑇 + 1; 𝑖𝑓 𝑛 > 1
(c) T (n) = 2
1; 𝑖𝑓 𝑛 = 1
𝑛−1
3𝑇 − 1; 𝑖𝑓 𝑛 > 1
(d) T (n) = 2
1; 𝑖𝑓 𝑛 = 1

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 14


Q41. Suppose we have a sorting algorithm that in addition to regular comparisons, is
also allowed super-comparisons: a super-comparison takes in three elements
and outputs those elements in order from smallest to largest. So, unlike a
regular comparison that only has two possible outcomes, a super-comparison
has 3! Possible outcomes. Which of the following is a correct lower bound on the
number of super-comparisons needed to sort an array of size n?
(a) log 2 𝑛! (b) log 3 𝑛! (c) log 6 𝑛! (d) 𝑛2

Q42. The Hamming distance between two n-bit vectors A and B is the number of
locations i such that A[i]  B[i]. What is the expected Hamming distance
between two random n-bit vectors (each location in each vector is determined by
a fair coin flip)?
(a)n/4 (b)n (c)n/2 (d)3n/4

Q43. Consider a random permutation of the numbers 1 . . . n. A number in this


permutation is called a Leader if it‟s greater than all the numbers to its left.
(Note that the number that ends up in the first position is definitely a Leader)
What‟s the probability that a number in position i is a Leader?
(a)1/𝑛 (b) 1/𝑖 (c) 1/ 𝑖 (d) 1/ 𝑛

Q44. The recurrence T (n) = T (n/3) + T (n/2) + n solves to:


(a)  (log n) (b)  (𝑛log 3 2 ) (c)  (n) (d)  (n log n)

Q45. [MSQ]
You need a SET implementation. Your application inserts elements a lot;
however, it rarely checks membership and it almost never removes elements.
Which of the following data structures seems most appropriate?
(a) AVL tree.
(b) (2, 4) tree.
(c) Unsorted linked list with unique elements.
(d) Unsorted linked list with repeated elements.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 15


Q46. Given the two statements below, which of them are true?
S1: Starting from vertex v0 in a graph, the time required by Depth-First Search
to find a path (if one exists) to some vertex v is less than that required by
Breadth First Search.
S2: The space required by Depth-First Search is less than that required by
Breadth First Search.
(a) Both statements are true
(b) S1 is true, but S2 is false
(c) S1 is false, but S2 is true
(d) Both statements are false

Q47. The worst-case performances of the heap operations deleteMin () and insert ()
are both O(log n). Given the two statements below, which of them are true?
S1: The experimentally found best case performance of deleteMin () is O (1).
S2: The experimentally found best case performance of insert () is O (1).
(a) Both statements are true
(b) S1 is true, but S2 is false
(c) S1 is false, but S2 is true
(d) Both statements are false

Q48. [MSQ]
Suppose a program P is O(N2), and a program Q is O(2N), and that currently
both can solve problems of size 100 in 1 hour. If the programs are run on
another system that executes exactly 256 times as fast as the original system,
what size problems will they be able to solve?
(a) Input size for Program P = 1600.
(b) Input size for Program Q = 1600.
(c) Input size for Program P = 108.
(d) Input size for Program Q = 108.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 16


Q49. Suppose you have 255 unique integers which are placed in a binary search tree
in following way such that:
The first 3 levels (levels 0, 1, and 2) each contain as many nodes as are possible
for that level. Beginning at levels 3, the tree begins to degenerate so that there
are 2 less nodes than would be possible given the number of nodes in level 2. At
each level, the same thing occurs, that is, there are two less nodes than would
be possible given the number of nodes at the previous level. The first 4 levels
(levels 0 to 3) are shown below:

How many levels this tree will have? __________

Q50. How many of the following statement is/are False? ___________


1. In an undirected graph, the shortest path between two nodes always lies on
some minimum spanning tree.
2. If all edges of the graph have different weight, then the highest weight
spanning tree is unique.
3. In Huffman coding, the item with the second lowest probability is always at
the leaf that is furthest from the root.
4. In Huffman coding, the item with the highest probability is always at a leaf
that is the child of the root.
5. If the frequencies of the letters a, b, c are equal, then a = 01, b = 10, c = 11 is
a valid Huffman code.
6. Adding an edge to a directed graph can decrease the number of strongly
connected components by at most 1.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 17


Q51. Consider the linked implementation of queues, the function A and function B
are used to empty the Queue:
void B (struct Queue * pq) void A (struct Queue * p q)
{ {
pq → front = NULL; pq→rear = pq→ front;
pq→ rear = NULL; while (pq→ rear)
} {
pq → rear = pq → rear → next;
free (pq→ front);
pq → front = pq → rear;
}
}

Which of the following is true about implementation B?


(a) Is faster than the function A, but it leave the memory occupied with data
that is neither needed nor reachable any more.
(b) Has syntax error.
(c) All of the above
(d) Exactly does as what function A does.
Q52. Given an array of n elements, you have to design an algorithm to find the k
smallest elements in sorted order. The time complexity of the best such
algorithm assuming comparison based sorting will be
(a) O (k log n)
(b)  (n + k log k)
(c)  (n2)
(d)  (n log k)
Q53. It is required to sort a large number of records of employee details based on the
employee IDs. Assuming the records are already sorted on the basis of employee
names and it is required to maintain names as the secondary sort order (similar
to order by id, name in SQL), which of the following sorting algorithms is/ are
the most appropriate for this?
(a) Merge Sort
(b) Selection Sort
(c) Quick Sort
(d) Heap Sort

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 18


Q54. [MSQ]
Which of the following is/ are TRUE about Huffman Coding?
(a) Huffman coding may become lossy in some cases.
(b) Huffman Coding does not always have an exact solution.
(c) In Huffman coding, no code is prefix of any other code.
(d) There exists a greedy algorithm to do Huffman coding.

Q55. [MSQ]
If the asymptotic time complexity of an algorithm P is greater than the
algorithm Q, which of the following statement is/are true?
(a) P will always take more time to execue than Q for all inputs.
(b) P will always take more time to execue than Q for all small inputs.
(c) P will always take more time to execue than Q for all large inputs.
(d) P will always take more time to execue than Q for all inputs above 1
million in size.
Q56. What is the tigthest upper bound time complexity of the following code, if f(n)
takes 𝜃 log 2 𝑛 ?
void main(){
int i, j, k, m, n, C = 0;
if (n<10000)
{
for (i = n, i > 0, i++)
for(j = 1, j <= n*n*n; j = j ++)
f(n);
}
else
{
for(k = 0; k < n; k++)
for(m = 1, m < k; m++)
C = C + m;
for(i = 0; i<C; i++) f(n);
}
}
(a)𝑂(𝑛3 )
(b)O(n2)
(c) O (n4)
(d) O(n3 log n)

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 19


Q57. Consider the following code:
int sum(int n)
{
int i, j , c = 0; s = 0;
for (i = 1; i < n+1; i = i + 1)
{
c = i; j = 1;
while (j <= c)
{
s = s + c + i;
j = j + 1;
}
}
return s;
}
What is the value returned by of above function sum(10)? ____________

Q58. [MSQ]
Consider the following code:
int foo (int n){
int s = 0;
while (n > 0) {
s = s + n;
n = n/2;
}
return s;
}
Which of the following statement is/are True?
(a) The returned value of the given function is  (log n).
(b) The returned value of the given function is  (n).
(c) The time complexity and space complexity are  (log n) and O (1)
respectively.
(d) The time complexity and space complexity are  (log n) and  (log
n)respectively.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 20


Q59. [MSQ]
Maximum Sub array sum problem is to find the sub array with maximum sum.
For example, given an array {15, - 18, -10, 35, -30, 40, 20} the maximum
subarray sum is 65. The best possible algorithm to compute the maximum
subarray sum will run in
(a)O (n)
(b) (n log n)
(c)O (n 𝑛)
(d)O (n2)
Q60. [MSQ]
You are given an array A of n elements. The best possible algorithm to find two
numbers in A which sum to the largest number in A will run in (assuming
comparison based sorting)
(a)O (n) (b) (n log n)
(c) (n2 log n) (d)O (n2)

Q61. Consider the code given below, which runs insertion sort:
void insertion sort (int arr[ ], int
size)
{
int i, j, value;
for (i = 1; i < size; i++)
{
value = arr [i];
j = i;
while ( _____)
{
arr [j] = arr [j – 1];
j = j – 1;
}
arr [j] = value;
}
}
Which condition will correctly implement the while loop?
(a) (j > 0)||(arr[j – 1] > value)
(b) (j > 0) && (arr [j – 1] > value)
(c) (j > 0) && (arr [j + 1] > value)
(d) (j > 0) && (arr [j + 1] < value)

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 21


Q62. [MSQ]
For quick sort pivot selection you are provided with an buggy implementation of
Median select, which can return any element between (n/3)rd smallest and
(2n/3)rd smallest in O (n) time. With this function to choose the pivot, which of
the following is/are TRUE for the Quick sort implementation?
(a) In best case it can run in O (n) time
(b) In worst case it will take O (n2) time
(c) In worst case it will take  (n log n) time
(d) In best case, it will run in  (n log n) time.
Q63. [MSQ]
Consider the following C function for arguments m, n > 1
int foo (int n, int m)
{
while (m != n)
{
if (m > n) {m = m –
n;}
else {n = n – m;}
}
return n;
}
Which of the following is/ are false about the above function?
(a) For any positive integers m, n the functions returns the least common
multiple of m, n
(b) The asymptotic time complexity and space complexity of the function are O
(log (max (m, n))) and  (log (min (m, n))) respectively.
(c) If m = 9, n = 13 the functions returns 1.
(d) If m = 12 and n = 8 then function returns 4.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 22


Q64. [MSQ]
Which of the following statements is/are TRUE?
(a) When 92% of the inputs is already in sorted order, insertion sort will do less
number of comparisons than merge sort for all sufficiently large inputs.
(b) If the number of inversions in an array is O (n), then insertion sort will be
having fewer comparisons than quick sort for all inputs.
(c) If the number of inversions in an array is O (n), bubble sort will be having
fewer numbers of comparisons than merge sort for all inputs.
(d)If the number of inversions in an array is O (n) heap sort will be having fewer
number of comparisons than insertion sort for all inputs.

Q65. You are given fifteen rings numbered from 1 to 15, and three pegs labeled A, B,
and C. Initially all the rings are on peg A, arranged from top to bottom in
ascending order of their numbers. The goal is to move all the rings to peg B in
the minimum number of moves obeying the following constraints:
i. In one move, only one ring can be moved.
ii. A ring can only be moved from the top of its peg to the top of a new peg.
iii. At no point can a ring be placed on top of another ring with a lower
number.
How many moves are required? ___________
Q66. Ram and Shyam play a game in which Shyam first thinks of a natural number
between 1 and 16385 . Ram must find out that number by asking Shyam
questions, but Shyam can only reply by saying “Yes” or “no”. Assume that
Shyam always tells the truth. What is the least number of questions that Ram
needs to ask within which he can always find out the number Shyam has
thought of? _____________

Q67. Consider an array A of size k with distinct element and the recursive quicksort
algorithm, [randomized algorithm] in which in each recursive call, a pivot is
chosen uniformly at random from the sub-array being sorted. If this randomized
algorithm is applied to an array A, then what is the probability that the smallest
and the largest elements in the array are compared during a run of the
algorithm?

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 23


1 1 1 2
(a) (b) (c) (d)
𝑘2 𝑘 𝑘 log 𝑘 𝑘

Q68. The number of different orders are possible for elements 1, 2, 3, 4, 5 to be


inserted in to emty AVL tree such that no rotation will be done and element „4‟
is root, are ______________
Q69. Which of the following functions, given by there recurrence, grows the fastest
asymptotically?
𝑛
(a) T(n) = 4T +5n
2
𝑛
(b) T(n) = 9T + 4 n2
3
𝑛
(c) T (n) = 4T + n (log n)2
4
𝑛
(d) T (n) = 20T + 2 (n log n)1.99
5

Q70. Which of the following functions asymptotically grows the fastest as n goes to
infinity?
(a) 2(log log n)! + (log log log n)!
(b) (log log n + log log log n)log n
(c) (log log n)log log log n
(d) (log n)log log n
Q71. Which of the following statements is TRUE for all sufficiently large integers n?
log log 𝑛
(a) 2 log log 𝑛
< 22 <2 𝑙𝑜𝑔𝑛
<n
log log 𝑛
(b) 2 log 𝑛
< n <2 log log 𝑛
< 22

(c) n <2 𝑙𝑜𝑔𝑛


<22 log log 𝑛
<2 log log 𝑛

log log 𝑛
(d) n <22 <2 log log 𝑛
<2 𝑙𝑜𝑔𝑛

Q72. How many of the following statement is/are true? ___________


i. In networking, when we want to broadcast some packets, we use the DFS
algorithm.
ii. In social networks, we can find people within a given distance „k‟ from a
person using Breadth First Search till „k‟ levels.
iii. DFS Algorithm Detects a cycle in a graph;
iv. The bridges in a graph can be found using DFS.
v. DFS algorithm also checks if a graph is bipartite or not.
vi. Using DFS, we can find the strongly connected components in a graph.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 24


Q73. Let G(V, E) be a weighted directed simple graph where |V|= n. If all the edge
weights are integers then the worst case time complexity to determine whether
there exists a path of arbitrarily large weight or not, is
[Select option in tightest upper bound]
(a)O (n)
(b)O (n. log (n))
(c) O (n1.5)
(d) O (n3)

Q74. [MSQ]
Consider the following directed graph

Suppose a depth – first traversal of this graph is performed, assuming that


whenever there is a choice, the vertex erlier in the alphabetical order is to be
chosen. Suppose the number of tree edges is T, the number of back edges is B,
the number of cross edges is C and the number of forward edges is F. Then
which of the following statement is/are true?
(a)T = 2B
(b)T = B(C + F)
(c) T = B + C + F
(d)T = 3B – 2(C+F) + 1

Q75. Consider the following C-program:

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 25


void fun(int x, int y)
{
int u, v;
u = y; v = x;
while (x  y)
{
if (x > y){
x = x – y,
v = v + u;
}
else if (y > x) {
y = y – x;
u = u + v;
}
}
printf (“%d”, (x + y) / 2);
printf (“%d”, (u + v) / 2);
}
If x and y are two positive integers then which of the following statement is
true?
(a) The program prints gcd (x, y) and the first prime larger than both x and y.
(b) The program prints gcd (x, y) followed by lcm (x, y)
(c) The program prints gcd (x, y) followed by ½  lcm (x, y)
(d) The program prints ½  gcd (x, y) followed by ½  lcm (x, y)
Q76. Consider the following function of C:
int wonder (int m)
{
int c = 0
while (m ! = 0){
m = m & (m – 1); // „&‟ is bit-wise anding operator
c = c + 1;
}
return c;
}
Assume „m‟ is an unsigned integer. What does the function wonder(n) return?
(a) It returns the biggest power of 2 dividing m, but zero if m is zero.
(b) It returns the one‟s complement of the binary representation of m.
(c) It returns the number of ones in the binary representation of m.
(d) The code might go into an infinite loop for some m.
Q77. Consider the following game. There is a list of distinct numbers. At any round, a

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 26


player arbitrarily choose two numbers a, b from the list and generates a new
number c by subtracting the smaller number from the larger one. Then the
numbers a and b are put back in the list. If the number c is non- zero and is
not yet in the list, c is added to the list. The player is allowed to play as many
rounds as the player wants. The score of player at the end is the size of the final
list.Suppose at the beginning of the game the list contains the following
numbers: 48, 99, 120, 165 and 279. What is the score of the best player for this
game? _________

Q78. Consider the following program modifying an n  n square matrix A:


void main()
{
int A[100][100], temp;
for (i = 1; i <= n; i++){
for (j = 1; j<= n; j++){
temp = A [i] [j] + 5;
A [i] [j] = A [j] [i];
A [j] [i] = temp – 5;
}
}
}
Which of the following statements about the contents of matrix A at the end of
this program must be TRUE?
(a) The new A is the transpose of the old A.
(b) All elements above the diagonal have their values incresed by 5 and all the
values below have their values decresed by 5.
(c) All elements above the diagonal have their values decresed by 5 and all the
values below have their values incersed by 5.
(d) A remains unchanged.

Q79. Let n  3, and let G be a simple, connected undirected graph with the same

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 27


number n of vertices and edges. Each edge of G has a distinct real weight
associated with it. Let T be the minimum weight spanning tree of G. Which of
the following statements is/are always true?
i. The minimum weight edge of G is in T.
ii. The maximum weight edge of G is not in T.
iii. G has a unique cycle C and the minimum weight edge of C is also in T.
iv. G has a unique cycle C and the maximum weight edge of C is not in T.
v. T can be found in O (n) time from the adjacency list representation of G.
(a) ii & v only (b) All except ii
(c) i, iii, v only (d) All the above
Q80. How many distinct weight spanning trees does the following undirected,
weighted graph have? ______________

Q81. [MSQ]
Consider the following recurrence relation:
𝑛 3𝑛
𝑇 + 𝑇 + 𝑛 𝑖𝑓 𝑛 ≥ 2
T (n) = 𝑘 4
1 𝑖𝑓 𝑛 = 1
Which of the following statements is/are True?
(a) If k = 3 then T (n) is O (𝑛 𝑛).
(b) If k = 3 then T (n) is O (n log n).
(c) If k = 4 then T (n) is O (n log n).
(d) If k = 5 then T (n) is O (n).

Q82. [MSQ]

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 28


Consider the following recurrence relation:
2𝑇 𝑛 + log 𝑛 𝑖𝑓 𝑛 ≥ 2
T (n) =
1 𝑖𝑓 𝑛 = 1
Which of the following statement is/are TRUE?
(a) T (n) is O (log n) (b)T (n) is O (log log n)
(c) T (n) is O (log n. log log n) (d)T (n) is O (log2n)

Q83. Let T (a, b) be the function with two arguments (both non-negative integral
power of 2) defined by the following recurrence:
𝑎 𝑏
 T (a, b) = T , 𝑏 + 𝑇 𝑎, 𝑖𝑓 𝑎, 𝑏 ≥ 2;
2 2
𝑎
 T (a, 1) = T , 1 𝑖𝑓 𝑎 ≥ 2;
2
𝑏
 T (1, b) = T 1, if b  2;
2
 T (1, 1) = 1
What is the value T (1024, 16)? __________________
Q84. Consider the following three version of the binary search program. Assume that
the array A is sorted.
int i, j, k, x, A[1. . . . N] ;
i = 1; j = N;
void Program1(){ void Program2(){ void Program3(){
while (a [k] == x) || (i > j)){ while(i > j){ while(i > j){
k = (i + j) / 2; k = (i + j) / 2; k = (i + j) / 2;
if (a [k] < x) i = k; if (x < a [k]) j = k – 1; if (x < a [k]) j = k;
else j = k; if (a[k] < x) i = k + 1; else i = k + 1;
} } }
} } }

A binary search program is called provided it terminates with a[k] = x whenever


such an element exist, or it terminates with a[k]  x if there exists no array
element with value x. Which of the following program is correct?
(a)Both Program 1 & 2 (b)Only Program 2 & 3
(c) Only Program 3 (c) None of the above

Q85. Suppose you are given an array A with 2n numbers. The numbers in even index

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 29


are sorted in ascending order, that is A[0]  A [2]  A[4]  . . .  A [2n-2]. The
numbers in odd odd are sorted in desending order that is A [1]  A [3]  . . .  A
[2n-1].What is the method you would recommend for determining if a given
numbers is in the array?
(a) Sort the array using quick sort and then use binary search.
(b) Merge the sorted lists and perform binary search.
(c) Perform a single binary search on the entire array.
(d) Perform seprate binary searches on the odd positions and the even positions.

Q86. Suppose you are given „n‟ numbers and you sort them in descending order as
follows:
1. First find the maximum. Remove this element from the list and, then
2. find the maximum of the remaining elements, remove this element,
and so on, until all elements are exhausted.
How many comparisons does this method require in the worst case?
[Select answer in tightest upperbound]
(a)O((log n)2)
(b)O (n2) but not better.
(c)O (n log n)
(d) O(n)

Q87. [MSQ]
Consider the quick sort algorithm on a set of n numbers, where in every
recursive subroutine of the algorithm, the algorithm choose the median of that
set as the pivot. Then in this case, which of the following statements is/are
TRUE?
(a) The running time of the algorithm is  (n2).
(b) The running time of the algorithm is  (n log n)
(c) The recurrence relation of the algorithm is T(n) = 2T(n/2) + (n)
𝑛 𝑛
(d) The recurrence relation of the algorithm is T(n) = 𝑇 −1 +𝑇 𝑛− + (n).
2 2

Q88. Suppose „100‟ processors are connected in a linear array as shown below. Each

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 30


processor has a number. The processors need to exchange numbers so that the
numbers eventually appear in ascending order (the processor P1 should have
the minimum value and the processor P100 should have the maximum value).

The algorithm to be employed is the following.


 Odd numbered processors and even numbered processors are activated
alternate steps; assume that in the first step all the even numbered
processors are activated.
 When a processor is activated, the number it holds is compared with the
number held by its right-hand neighbour (if one exists) and the smaller of
the two numbers is retained by the activated processor and the bigger stored
in its right hand neighbour.
How long does it take for the processors to sort the values?
(a)100 log 100 steps (b)10000 steps
(c) 100 steps (d) 10 steps

Q89. [MSQ]
An array of k distinct elements is said to be un-sorted if for every index i such
that
2  i  k – 1, either A[i] > max {A[i – 1], A [i + 1]} or A[i] < min {A[i – 1], A[i + 1]}.
Then which of the following statement is/are true?
(a) The array B is un-sorted array according to given definition; where B[] = {3,
7, 5, 8, 4, 6, 2, 1}.
(b) The array C is not un-sorted array according to given definition; where
C [] = {7, 6, 5, 4, 3, 8, 1}.
(c) The time complexity of the fastest algorithm that takes as input a sorted
array A with k distinct elements and un-sorts A, O (n log n) but not O (n).
(d) The time complexity of the fastest algorithm that takes as input a sorted
array A with k distinct elements and un-sorts A, is O(n) but not O ( 𝑛)

Q90. Consider a connected weighted simple graph G = (V, E) with the weight function

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 31


w: E  R on its edge set; where |V| = n and |E|= m. If E = {e1, e2, . . .em } such
that their weights are in decreasing order i.e., 𝑤 𝑒1 > 𝑤 𝑒2 > 𝑤 𝑒3 > ⋯ >
𝑤 𝑒𝑚 .
How many of the following statements is/are True? ____________
[Assume edge-weights are distinct and n>=3]
i. The edges e1 has to be present in every maximum weight spanning tree.
ii. Both e1 and e2 have to be present in every maximum weight spanning
tree.
iii. The edge em has to be present in every minimum weight spanning tree.
iv. The edge em is never present in any maximum weight spanning tree.
v. G has a unique maximum weight spanning tree.

Q91. Consider a connected weighted graph G = (V, E) where |V|= n, |E| = m. If all
the edges have distinct positive integer weights, then the maximum number of
minimum weight spanning trees in the graph is
(a) nn – 2 (b) m (c) n (d) 1

Q92. [MSQ]
Consider the following undirected graph with some edge costs missing.

Suppose the wavy edges form a Minimum Cost Spanning Tree for G. Then,
which of the following inequalities always holds?
(a) 𝑐𝑜𝑠𝑡 𝑎, 𝑏 ≥ 6
(b) 𝑐𝑜𝑠𝑡 𝑏, 𝑒 ≥ 5
(c) 𝑐𝑜𝑠𝑡 𝑒, 𝑓 ≥ 5
(d) 𝑐𝑜𝑠𝑡 𝑎, 𝑑 ≥ 4

Q93. [MSQ]

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 32


Consider an undirected connected simple graph G = (V, E) with the weight
function w: E  R on its edge set. Let w (e1) < w (e2) < . . .< w (em) where E =
{e1, e2 . . .em}. If T is the minimum spanning tree of G then which of the
following statements is/are True?
(a) The tree T always contains the edge with smallest weight i.e, e1.
(b) The tree T always contain the edge with 2nd smallest weight i.e, e2.
(c) The minimum weight edge incident on each vertex has to be present in T.
(d) T is the unique minimum spanning tree in G.

Q94. Consider the following undirected connected graph G with weights on its edges
as given in the figure below.

Consider the following two definitions:


 A minimum spanning tree is a spanning tree of least weight and a
maximum spanning tree is one with largest weight.
 A second best minimum spanning tree whose weight is the smallest among
all spanning trees that are not minimum spanning trees in G.
Which of the following statements is TRUE in the above graph? (Note that all
the edge weights are distinct in the above graph)
(a)There is a unique minimum spanning tree, however there is more than one
maximum spanning tree here.
(b) There is unique minimum spanning tree, however there is more than one
second-best minimum spanning tree here.
(c) There is more than one minimum spanning tree, however there is a unique
maximum spanning tree here.
(d) There is more than one minimum spanning tree and similarly, there is more
than one second-best minimum spanning tree here.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 33


Q95. Consider the following code fragment in the C programming language when run
on a non-negative integer n.
int f (int n)
{
if (n = = 0 || n = = 1)
return 1;
else
return f (n – 1) + f (n –
2);
}
Assuming a typical implementation of the language, what is the running time of
this algorithm and how does it compare to the optimal running time for this
problem?
(a) This algorithm runs in polynomial time in n but the optimal running time is
exponential in n.
(b) This algorithm runs in exponential time in n and the optimal running time is
exponential in n.
(c)This algorithm runs in exponential time in n but the optimal running time is
polynomial in n.
(d)This algorithm runs in polynomial time in n and the optimal running time is
polynomial in n.

Q96. Four matrices M1, M2, M3 and M4 of dimensions p  q, q  r, r  s and s  t


respectively can be multiplied is several ways with diffrent number of total
scalar multiplications. For example when we multiplied ((M1 M2)  (M3  M4)),
then the total number of multiplication is pqr + rst + prt.
If p = 10, q = 25, r = 20, s = 15 and t = 40 then the minimum number of scalar
multiplications needed, is______________

Q97. An unordered list contains n distinct elements. The number of comparisons to


find an element in this list that is neither maximum nor minimum is
(a) Θ(n log n) (b) Θ(log n) (c) Θ(n) (d) Θ(1)
Q98. Consider the following pseudo code:

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 34


void main()
{
int i, j , k, D = 2;
for (i = 1; i<= n; i++)
for (j = i; j<=n; j++)
for (k = j + 1; j<=n; j++)
D = D * 3;
}
What is the total number of multiplications to be performed?
(a)Half of the product of the 3 consecutive integers.
(b)One-Sixth of the product of the 3 consecutive integers.
(c)One-Third of the product of the 3 consecutive integers.
(d) None of the Above

Q99. Which of the following is the best running time to sort n integers in the range 0
to n2−1?
(a) O(log n) (b)O(n) (c)O(n log n) (d)O(n2)
Q100. Arrange the following in non – incresing order of asymptotic complexity.
I. The number of ways to seat n people around a circular table.
II. The number of four element subsets of a set of size n.
III. The time to find a given element in a sorted array of length n.
IV. The time to find a given element in a doubly linked list of length n.
V. The biggest number that can be represented with n bits.
VI. Time to sort a list of n items if you are only allowed to swap adjacent
elements.
VII. The number of edges in a tree with n nodes.
(a)VII < III < IV < VI < II < V < I (b) III < VII < IV < VI < II < V < I
(c) III < VII < IV < VI < II < I < V (d) III < VII < IV < II < VI < V < I
Q101. An e – commerce site on search for a particular keyword returns the search
results in order of popularity by default, also the user is allowed to sort by price
(low to high) or (high to low) however if two or more products are of the same
price then among the same price products should be presented to the user in
the same oder of popularity. Which of the following sorting algorithm cannot be
used for such a scenario?
(a)Merge sort (b) Bubble sort (c)Quick sort (d)Insertion sort
Q102. An empty queue is given in which 5 elements are going to be inserted then for

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 35


enqueue it is taking 3 sec and for dequeue it is taking 3 seconds, and there is
time elapse between enqueue – enqueue and dequeue – dequeue of 1 sec. And
for enqueue – dequeue is of 2 seconds. Then what is the total time of all the 5
elements (from enqueuing all elements to dequeuing all)? __________

Q103. Consider two vertices x and y that are simultaneously on the FIFO queue at
some point during the execution of breadth first search from s in an undirected
graph. Which of the following are true?
I. The number of edges on the shorest path between s and x is at most one more
than the number of edges on the shorest path between s and y.
II. The number of edges on the shorest path between s and x is at least one less
than the number of edges on the shorest path between s and y.
III.There is a path between x and y.
(a) I only (b)I and II only (c)I and III only (d)None
Q104. An element in an array X is called a owner if it is grater than all elemnt to the
right of it in X. The best algorithm to find all owner in an array.
(a) Solves it in linear time using a left to right pass of the array.
(b) Solves in linesr time using a right to left pass of thr array.
(c) Solve it using divide conquer in time  (n log n)
(d) Solves it in time  (n2)

Data for next six questions:


For each question, we have a partial spanning tree. Determine whther it could possibly
be obtained from (a prematurely stopped) Prim‟s Algorithm, (a prematurely stopped)
Kruskal‟s Algorithm, both or neither.
Q105.

(a)Prim‟s (b) Kruskal‟s (c) Both (d)Neither

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 36


Q106.

(a)Prim‟s (b) Kruskal‟s (c) Both (d)Neither

Q107.

(a)Prim‟s (b) Kruskal‟s (c) Both (d)Neither

Q108.

(a)Prim‟s (b) Kruskal‟s (c) Both (d)Neither


Q109.

(a)Prim‟s (b) Kruskal‟s (c) Both (d)Neither

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 37


Q110.

(a)Prim‟s (b) Kruskal‟s (c) Both (d)Neither


Q111. Consider the following method runs on a BST:
void doSomething (struct tree* node)
{
current = node;
prev = node;
if (current→left == NULL) && (current→right == NULL)
{
print("No such key);
return;
}
else
{
while (current →left != NULL) {
prev = current;
current = current →left;
}
if (current →right == NULL)
{
print(prev→key);
return;
}
else {
current = current→right;
while (current→left !=NULL)
current = current→left;
}
print(current→key);
}}
What does above method prints?
(a) Largest key of BST
(b) Second Largest key of BST
(c) Second smallest key of BST
(d) Smallest key of BST

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 38


Q112. Consider the following recursive function:
int mystery (int n, int k)
{
if (k == 0 || k == n) { return 1; }
else if (k > n) { return 0; }
else { returnmystery(n - 1, k - 1) + mystery(n -
1, k); }
}
What value will be returned by mystery (7, 1)? ____________

Q113. [MSQ]
Consider the following code. What is the time complexity of the following
program?
int pow (int x, int n)
{
if ( n == 1 ) return x;
else {
if ( n % 2 == 0 ) return pow (x, n/2) * pow (x, n/2) ;
else return pow(x, n/2)*pow(x, n/2) *x;
}
}
(a)O(n log n) (b) O(log n) (c) O(n2) (d) O(n)

Q114. [MSQ]
Which of the following statements is/are correct?
(a)Merge sort, Quick sort and Bucket sort are comparison-based sorting
algorithms.
(b)A reverse-sorted array (i.e., decreasing order) is always a max-heap.
(c) In a directed graph with positive edge weights, the edge with minimum
weight belongs to the shortest paths tree for any source vertex.
(d) Given any weighted directed graph with all distinct edge weights and any
specified source vertex, the shortest paths tree is unique.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 39


Q115. Given the following weighted, directed graph, use Dijkstra‟s algorithm for single-
source(if source vertex is s) shortest paths and find the order in which vertices
are visit by the algorithm?(Assume if two vertex have same distance than visit
vertex in alphabetically order)

(a) s, r, t, x, z, y (b) s, r, t, y, x, z
(c) s, r ,t, y, z, x (d) s, t, r, y, x, z
Q116. Suppose we wanted to implement a stack using queue. Two queues Q1 and Q2
are used. One of the queues will be flagged as active at each time instant. To
push into our stack, we insert into queue flagged as active, say it is Q1. To pop
from our stack we insert into Q2 all of the elements of active queue Q1 except
for the last element, which we pop. Then we flag Q2 as active.
Here is pseudo code, assuming currently Q1 is flagged as active:
 push(x): Q1.insert(x)
 pop(): delete all elements from Q1 and insert into Q2 except last. Return the
last element
 flag Q2 as active
Let‟s say that cost of each insert and delete operation is Rs1.Suppose that
(starting from an empty stack) we insert 5 elements, in stack in order 1 to 5 and
want to delete element 5 . What is the total cost of these 6 operations? ________

Q117. You are given the following eight numbers: 6, 2, 9, 1, 3, 7, 16, 11. In what order
these numbers should be placed in the array A = {A[1], ..., A[8]} so that
8 𝑖
( 𝑖=1 𝑗 =1 𝐴[𝑗]) is minimized?
(a)In ascending order
(b)In descending order
(c)In any order
(d)Order doesn‟t matter sum is always same

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 40


Q118. Given are two algorithms for merging two sorted arrays.
Which of the following is correct implementation for merging two sorted arrays?
Algorithm A Algorithm B
initialize i, j and k to if (n  m) {
zero for (i = 0; i < m; i++)
while (i < n and j < m) { c [i] = b [i];
if (a [i]  b [j]) { for (i = 0; i< n; i++)
c [k] = a [j]; value = a [i];
increment I; k = m – 1;
increment k;} while ((k > 1) and (c [k] > value) {
else{ c [k] = c [k – 1];
c [k] = b [j]; k - -;}
increment j; c [k] = value;
increment k; } }
} else{
if (i = n) { for (i = 0; i < n; i++)
for (p = j; p < m; c [i] = a [i];
p++){ for (i = 0; i < m; i++)
c [k] = b [p]; value = b [i]
increment k;} k = n – 1;
} while ((k > 1) and (c [k] > value)){
if (j = m) { c [k] = c [k – 1];
for (p = i; p < n; k--;}
p++){ c [k] = value;
c [k] = a [p]; }
increment k; }
}
a[n] and b[m] are global sorted array and c[n+m] is temporary array. i, j, k
integers storing the indexes of array a,b,c;
(a) Both algorithms are correct
(b)Algorithm A is correct but Algorithm B is not
(c) Algorithm B is correct but Algorithm A is not
(d) Both Algorithms are not correct

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 41


Q119. Refer to the diagram below of a singly linked list

Suppose we implement a queue with a singly linked list. Which end of the list
should items be dequeued efficiently?
(a) first node (b) first or last node
(c) any where it doesn‟t matter (d) last node

Q120. Refer to the diagram below of a singly linked list

Suppose we implement a stack with a singly linked list. Which end of the list
should items be popped efficiently?
(a)first node (b) first or last node
(c) any where it doesn‟t matter (d) last node

Q121. Suppose you have an ordered linked list of integers to put an element into the
list you scan the list to find the correct insertion point and then modify pointers
to do insertion.

If you only remove items from the front of the list, what is the big – O time
complexity for inserting items into and removing items from the list?
insert remove
(a) O (n) O (n)
(b) O (n2) O (n)
(c) O (1) O (n)
(d) O (n) O (1)
ADVANCED DATA STRUCTURES AND ALGORITHMS Page 42
Q122. Consider the following code
void delet(struct node * head , int pos)
{
struct node* p1;
int c=1;

if( head==NULL) return;


else if(pos==1 &&headnext ==NULL)
{
free(head);
head=NULL;
}
else if(pos==1 && head->next!=NULL)
{
cur=head;
head=head->next;
cur->next=NULL;
free(cur);
}
else
{
p1=head;
while(c<pos)
{
cur=next;
p1=p1->next;
c++;
}
cur->next=p1->next;
p1->next=NULL;
if(next==NULL)
free(next);
}
}
What function does above code implements?
(a)The code delete an element from desired position of linked list
(b)The code deletean head element from linked list
(c) The code deletean last element from linked list
(d)The code do some other function

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 43


Q123. A list of integers can be stored sequentially in an array. The list can be
maintained in sorted order. Maintaining the list in sorted order in an array
leads to inefficient (linear) execution for which of the following operations?
(I) Inserting and deleting elements
(II) Printing the list
(III) Computing the average of the elements
(a) II only
(b)II and III only
(c)I and III only
(d)I, II, and III
Q124. The factorial of an integer is the product of that integer multiplied by all the
positive non-zero integers less than that integer. So, 5! (! is the mathematical
symbol for factorial) is 5 * 4 * 3 * 2 * 1. 4! is 4*3*2*1, so 5! could be written as
5*4!.So a recursive definition of factorial is n! is n*(n-1)! , as long as n >1. 1! is
1. What is the recursive call for this function (fact)?
(a)fact(n)*n;
(b)fact(n-1)*n;
(c)(n-1)*fact(n)
(d)fact(n-2)*(n-1)
Q125. Consider the following code
int zeal(struct tree * node)
{
if (nodeleft ==NULL &&noderight ==NULL)
return 0;
else
return 1 + zeal (nodeleft)+zeal(noderight);
}
When this code runs on a binary tree what it will return?
(a) the number of nodes in the tree
(b) the length of the shortest path from the root to a leaf
(c) the length of the longest path from the root to a leaf
(d) the number of internal (non-leaf) nodes
(e)

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 44


Q126. Refer to method zeal:
int zeal (TreeNode *tree) {
int x, y;
if (tree = = null)
return -1;
else {
x = 1 + zeal (treeLeft());
y = 1 + zeal (treeRight());
if (x > = y)
return x;
else
return y;
}}
method zeal returns -1 for an empty tree.
What does method zeal do when invoked for a nonempty tree?
(a) It returns the largest value in the tree.
(b) It returns the number of nodes in the subtree that has the greatest number
of nodes.
(c) It returns the level of the tree.
(d) It returns either the leftmost value or the rightmost value of a tree,
whichever is larger.

Q127. There is a set of 5 jobs each of which runs in unit time. Each job has a integers
valued deadline time (3, 2, 1, 3, 3) and real valued penalty (8.2, 6.3, 7.1, 4.2,
3.2) respectively what is the minimum total penalty Incurred if greedy approach
is used ?
(a) 10.2 (b) 9.4 (c) 8.5 (d) 7.4
Q128. Here are four additional algorithms whose runtime functions (we‟ll also call
them T2, T3, T4, T5, satisfy the four recurrences,
Tk(n) = 1, for n = 1,
Tk(n) = kTk(n/2) + O(n), for n >1, for k = 2, 3, 4, 5.
Which best describes the situation?
(a) Tk(n) = O(n), for each k in 2….5.
(b) Tk(n) = O(nlg(n)), for each k in 2….5.
(c) Tk(n) = O(nlog(5)), for some k in 2….5.
(d) none of the above

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 45


Q129. You have a singly linked list constructed out of nodes defined as follows:
Struct node
{
int data;
struct node * next;
}
In all of functions shown below, the parameter first refers to the first node in the
linked list, if there is one and has the value null otherwise. Which of the
following functions correctly inserts a value x at the front of the linked list and
returns a reference to the new front of the linked list?
I. struct node * insert front (node * first, int x)
{
first = (struct node*) malloc (size of (struct node));
first  data = x;
first  next = first;
return first;
}

II. struct node * insert front (struct node * first, int x)


{
Struct node * n = (struct node *) malloc (size of (struct node));
n  data = x;
n  next = first;
first = n;
return first;
}
struct node * insert front (struct node * first, int x)
{
struct node * n = (struct node *) malloc (size of (struct node));
first = n;
n  data = x;
n  next = first
return first;
}
(a)II only
(b) III only
(c)I and II only
(d)II and III only

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 46


Q130. Consider method selection_Sort below. Assume that method swap exists, which
swaps the array elements of intArray according to the provided index
parameters.
void selection_Sort()
{
for (int p = 0; p < intArray.length-1; p++)
{
int temp = p;
for (int q = p+1; q <intArray. Length; q++)
{
if (intArray[q] <intArray[temp])
temp = q;
}
if (intArray[p] != intArray[temp])
swap(p, temp);
}
}
Assume that intArray stores a random list of integers prior to calling
selection_Sort. How will the integers be arranged after a call to selection_Sort?
(a) Data will be arranged in ascending order.
(b) Data will be arranged in descending order.
(c) The largest integer will be stored in last array element.
(d) The smallest integer will be stored in the first array element.

Q131. A Huffman tree is constructed for a text document containing 5 characters. The
character „e‟ appears most frequently, and the character „i‟ has the lowest
frequency. Which of the following could be the Huffman tree for this document?

(a) I only (b)II only (c) III only (d) either II or III

Q132. Consider an algorithm A and following table containing time to execute A


ADVANCED DATA STRUCTURES AND ALGORITHMS Page 47
according to different input size:
Input size (n) Approximate Time (in sec) to execute A
1000 0.75
2000 1.06
4000 1.5
8000 2.12
16000 3
32000 4.24
64000 6
What will be the time complexity of A?
(a)𝜃(𝑛) (b) 𝜃(log 𝑛) (c) 𝜃(𝑛 log 𝑛) (d) 𝜃 𝑛0.5
Q133. A set of n keys: {k1, . . . ,kn} is to be stored in an initially empty binary search
tree. Which
of the following statements is always true?
(a) The resulting binary search tree has the same height, regardless of the order
in which the keys are inserted in the tree.
(b) If ki is the largest key, then in every binary search tree storing the above set
of keys, the right child of the node storing ki is null.
(c) A preorder traversal of the tree visits the keys in increasing order of value.
(d) After inserting the keys, the key stored at the root of the tree is the same
regardless of the order in which the keys are inserted.
Q134. Consider the following recursive algorithm Two Consecutive Head, which take
as input a positive integer x (𝑥 ≥1):
Algorithm Two_Consecutive_Head (x):
//all coin flips made are mutually independent
flip a fair coin twice;
if the coin came up heads exactly twice, then return 2x
else Two_Consecutive_Head (x+1)
end if
You run algorithm Two_Consecutive_Head (1). Define the random variable X to
be the value of the output of this algorithm. What will be the value of probability
P(X=2k)?(Assume that probability of getting head = 1/2).
(a)(1/4)k.3/4 (b) (1/4)k-1.3/4
(c) (3/4)k.1/4 (d)(3/4)k-1.1/
Q135. A message is made up entirely of characters from the set X = { A, B, C, D, E, F,

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 48


G} The table of probabilities of each character is shown below:
Character A B C D E F G
Probability 1/24 7/120 1/3 1/4 1/6 1/15 1/12
A message of 600 characters over X is encoded using Huffman coding. Then the
excepted length of the encoded message in bits is __________

Q136. A k-ary tree is a tree in which every node has at most k children. In a k-ary tree
with n nodes and height h, which of the following is an upper bound for the
maximum number of leaves (M) in terms of n, h or k? (Assume root is at height
0)
𝑛𝑘 −𝑛+1
(a) M= logk n (b) M = logn k (c) M = (d) M= hk
𝑘

Q137. Given a binary search tree which is also a complete binary tree. The problem is
to convert the given BST into a Min Heap with the condition that all the values
in the left subtree of a node should be less than all the values in the right
subtree of the node. This condition is applied on all the nodes in the so
converted Min Heap.

What will be the worst case time (tightest) complexity of given problem, if you
can take Auxiliary space of O(n)?
(a)𝑂(log 𝑛) (b)𝑂(𝑛) (c)𝑂(𝑛2 ) (d) 𝑂(𝑛 log 𝑛)
Q138. Your first run of a data with a set size of 1 takes 1 ms to process. Increasing
the set size to 2 takes 2.83ms to process. Increasing the set size again to 3
takes 5.52ms to process. Increasing the set size again to 4 takes 8ms to
process. Finally, you increase the set size to 5 and it takes 11 ms to process. If
the size =100 then how much time will take by this algorithm (approx.)?
______________
Q139. Consider an array A of n distinct integers (positive or negative), we have to

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 49


design algorithm to find an index i such that A[i] = i.Let f(n)be time complexity of
optimal algorithms when given array is sorted and g(n) be time complexity of
optimal algorithms when given array is unsorted. What is tightest upper bound
of [f(n)+ g(n)]?
(a)O(n) (b)O(log n) (c)O(n log n) (d)O(n2)

Q140. What is the expected number of comparisons required to perform a successful


sequential search for an element in an array on n integers?
(a)(n-1/2) (b)(n) (c) (n+1)/2 (d)(n/2)

Q141. Consider the following algorithm for searching for a given number x in an
unsorted array A[ 1...100] having 100 distinct values:
1. Choose an i at random from 1, 2, 3..100
2. If A [i] = x then Stop else Goto 1;
Assuming that x is present in A, what is the expected number of comparisons
made by the algorithm before it terminates?
(a)100 (b) 200 (c) 99 (d)50

Q142. Consider an array A[n] of n integers such that first k elements of array are
sorted is increasing order and remaining next elements are sorted in decreasing
order. (Ex: A = {1, 4, 5, 4, 3, 1, 1}, n = 7, K = 3). What is the worst case
complexity of a function Sort (A, n , k) to sort the above array?
(a)O(k) (b)O(n) (c)O(min(n, k)) (d)O(nk)
Q143. An array contains n characters of English alphabet (a , b , c….y , z). What is
the time complexity of finding the k times repeated character?
(a)O(n) (b)O(k) (c)O(k log n) (d)O(kn)
Q144. Consider an array sorted in descending order. We are running Quick sort on the
given array,
(i)Selecting first element as pivot.
(ii) Selecting median of first, middle and last element.
What will be the runtime complexity of above two cases respectively?
(a)O(n log n), O(n2) (b)O(n2) , O(n2)
(c) O(n2) , O(n log n) (d)O(n log n) , O(n log n)
Q145. Consider S ={ a/20 , @/15 , z/15 , e/45 ,l/5} .(where x/y represent y is

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 50


Frequency of x). How many distinct codes are possible for string “@zeal”, when
we use optimal code calculated as in S (by Huffman coding )._____________
Q146. A Consider a situation where swap/shift operation is very costly. Which of the
following sorting algorithms should be preferred so that the numbers of swap
operations are minimized in general?
(a) Heap Sort
(b) Selection Sort
(c) Insertion Sort
(d) Merge Sort

Q147. Consider the following code


{
int x = 0;
𝑓𝑜𝑟 𝑖𝑛𝑡 𝑖 = 2; 𝑖 ≤ 𝑛; 𝑖 = 𝑖 𝑖
x++ ;
}
What is the time complexity of above code? [Select option in tightest upper
bound]
(a)O(log(log*n))
(b)O(log log n)
(c)O(log n)
(d)O((log n)log n)
Q148. [MSQ]
In DFS traversal algorithm of a graph G, if d[u] is the time at which exploration
of a vertex u started, and f[u] is the time at which exploration of vertex u is
finished, and (u, v) represent directed edge from u to v. Then which of the
following statement is true?
(a)if edge (u,v) is forward edge then d[u]<d[v]<f[v]<f[u].
(b)An edge (u,v) is cross edge iff d[u]<f[u]<d[v]<f[v].
(c)if edge (u,v) is back edge then d[v]<d[u]<f[u]<f[v].
(d)None of the above

Q149. Consider the following program implementing matrix chain multiplication.

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 51


int mat_chain_multiplication(int *mat, int n)
{
int arr[n][n];
int i, k , row , col , len;
for(i=1 ; i<n ; i++)
arr[i][i] = 0;

for(len = 2; len< n; len++)


{
for(row = 1; row <= n - len + 1; row++)
{
col = row + len - 1;
arr[row][col] = INT_MAX;
for(k = row; k <= col - 1; k++)
{
int tmp = ______________;
if(tmp < arr[row][col])
arr[row][col] = tmp;
}
}
}
return arr[1][n - 1];
}
int main() {
int mat[6] = {20,5,30,10,40}; //order of 5 square matrix
int ans = mat_chain_multiplication(mat, 5);
printf("%d", ans);
return 0;
}
Which of the following lines should be inserted to complete the above code?
(a) arr[row][k] – arr[k + 1][col] + mat[row – 1] * mat[k]*mat[col];
(b) arr[row][k] + arr[k + 1][col] – mat[row – 1] * mat[k]*mat[col];
(c)arr[row][k] + arr[k + 1][col] + mat[row – 1] * mat[k] * mat[col];
(d) arr[row][k] – arr[k + 1][col] – mat[row – 1] * mat[k] * mat[col];

Q150. Consider an array of 0‟s and 1‟s and if we are applying Insertion sort on this
array in ascending array then how many comparisons (Let number of

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 52


comparisons = x) are performed between n elements of an array?
(a)𝑛 − 1 ≤ 𝑥 ≤ 𝑛 − 1 𝑛 − 2 /2
(b) 𝑛 − 1 ≤ 𝑥 ≤ 𝑛 − 1 𝑛 − 2 /2
𝑛2
(c)𝑛 − 1 ≤ 𝑥 ≤ +𝑛−2
4
𝑛2
(d) 𝑛 ≤ 𝑥 ≤
4

Q151. Given an unsorted array. The array has this property that every element in
array is at most k distance from its position in sorted array where k is a positive
integer smaller than size of array. Which sorting algorithm can be easily
modified for sorting this array and what is the obtainable time complexity?
(a) Insertion Sort with time complexity O(kn)
(b)Heap Sort with time complexity O(nLogk)
(c) Quick Sort with time complexity O(kLogk)
(d) Merge Sort with time complexity O(kLogk)

ADVANCED DATA STRUCTURES AND ALGORITHMS Page 53

You might also like