Advanced DSA Sheet 2023
Advanced DSA Sheet 2023
ADVANCED
DATA STRUCTURES
AND
ALGORITHMS
Practice Questions
Booklet
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).
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
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
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)
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? ______________
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.
(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
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
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
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
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
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.
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.
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)
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.
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)
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?
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
log log 𝑛
(d) n <22 <2 log log 𝑛
<2 𝑙𝑜𝑔𝑛
Q74. [MSQ]
Consider the following directed graph
Q79. Let n 3, and let G be a simple, connected undirected graph with the same
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]
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;
} } }
} } }
Q85. Suppose you are given an array A with 2n numbers. The numbers in even index
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
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
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]
Q94. Consider the following undirected connected graph G with weights on its edges
as given in the figure below.
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
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)
Q107.
Q108.
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.
(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
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
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;
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
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
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
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
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
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)