algorithms
Question 1 |
A | f ( n ^2 ) ( f ( n ) ^2 ), when f ( n ) is a polynomial |
B | f ( n ^2 ) o ( f ( n ) ^2 ) |
C | f ( n ^2 ) O ( f ( n ) ^2 ), when f ( n ) is an exponential function |
D | ![]() |
f(n)=n^c where c is a constant
f(n^2) = (n^2)^c = n^2c
(f(n))^2 = (n^c)^2 = n^2c
f(n^2) = (f(n))^2 is TRUE asymptotically.
Option-B: FALSE: The small omega function indicates the tightest upper bound.
f(n)^2 < o(f(n)^2) is FALSE asymptotically.
Option-C: FALSE: Consider f(n)=logn
f(n^2)=logn^2 = 2*logn
(f(n))^2 = (logn)^2 = logn * logn
f(n^2) <=Ω(f(n))^2 is FALSE asymptotically.
Option-D: FALSE:
Exponential values(Eg: 2^n, 3^n,..,k^n where k is a constant ”
f(n)=3^n
f(n^2)=f(3^(n^2)
(f(n))^2 = (3^n)^2 = 3^2n
f(n^2) >= O(f(n))^2 is FALSE asymptotically.
Question 2 |
A | m/n |
B | n/m |
C | 2n/m |
D | n/2m |
How?
For 1st element the probability of key1 ends up in slot 1 is 1/m.
For 2nd element the probability of key2 ends up in slot 2 is 1/m
..
..
For nth element the probability of keyn ends up in slot n is 1/m
Hence expected number of elements in a slot is:
1/m+1/m+... (n times)= n/m
In the given question h1 is for elements at even sequence position and h2 is for odd number of sequence positions. This will not affect the overall probability.
Hence here also it is n/m
Question 3 |
A | 509 |
Now, the 3^rd largest number will be at location of max3. We can find what can be the probable index values of max1 at different levels. They will be 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023 . . .
∴ 511 is index value of our max1
510 is index value of our max2
509 is index value of our max3
& Hence, Answer is 509
Question 4 |
A | The edge with the second smallest weight is always part of any minimum spanning tree of G . |
B | One or both of the edges with the third smallest and the fourth smallest weights are part of any minimum spanning tree of G . |
C | ![]() |
D | G can have multiple minimum spanning trees. |
Option-A: TRUE: As per the above graph, the second minimum edge weight is also part of the MST.
The second smallest weight is always in MST because it will not form a cycle.
Option-B: TRUE: Graph G is having more than 4 vertices. Suppose 3rd smallest element is forming a cycle then it takes 4th smallest element. So, the given statement is TRUE.
Option-C: TRUE: As per the example graph, it is always correct.
Option-D: FALSE: We will get a unique minimum spanning tree if edge weights are distinct.
Question 5 |
Then, which of the following statements is/are TRUE?
A | f (2^ n -1) 2^ n -1 |
B | f (2 ^n ) 1 |
C | f (5 . 2 ^n ) 2^ n + 1 1 |
D | f (2^ n + 1) 2 ^n + 1 |
Based on the “n” value we can get Option-A, B and C are correct.
Question 6 |
A | 24 |
Graph have 3 elements --> We get 2 MSTs
Graph have 4 elements --> We get 6 MSTs (3*2*1)
Graph have 5 elements --> We get 24 MSTs(4*3*2*1)
Question 7 |
FORTRAN implementation do not permit recursion because
A | they use static allocation for variables |
B | they use dynamic allocation for variables |
C | stacks are not available on all machines |
D | it is not possible to implement recursion on all machines |
→ Recursion requires dynamic allocation of data.
Question 8 |
The recurrence relation that arises in relation with the complexity of binary search is:
A | T(n) = T(n/2) + k, k a constant |
B | T(n) = 2T(n/2) + k, k a constant |
C | T(n) = T(n/2) + log n |
D | T(n) = T(n/2) + n |
∴ T(n) = 2T(n/2) + k, k a constant
Question 9 |
Which of the following algorithm design techniques is used in the quicksort algorithm?
A | Dynamic programming |
B | Backtracking |
C | Divide and conquer |
D | Greedy method |
Question 10 |
In which one of the following cases is it possible to obtain different results for call-by reference and call-by-name parameter passing methods?
A | Passing a constant value as a parameter |
B | Passing the address of an array as a parameter |
C | Passing an array element as a parameter |
D | Passing an array following statements is true |
{ ........
a[ ] = {1, 2, 3, 4}
i = 0
fun(a[i]);
print a[0];
}
fun(int x)
{
int i = 1;
x = 8;
}
O/p:
Call-by-reference = 8
Call-by-value = 1
Question 11 |
Which one of the following statements is false?
A | Optimal binary search tree construction can be performed efficiently using dynamic programming. |
B | Breadth-first search cannot be used to find connected components of a graph. |
C | Given the prefix and postfix walks over a binary tree, the binary tree cannot be uniquely constructed. |
D | Depth-first search can be used to find connected components of a graph. |
Question 12 |
Consider the following two functions:
Which of the following is true?
A | g1(n) is O(g2(n)) |
B | g1 (n) is O(3) |
C | g2 (n) is O(g1 (n)) |
D | g2 (n) is O(n) |
E | Both A and B |
Growth rate of g1 is less than that of g2 i.e., g1(n) = O(g2(n)) = O(n).
Question 13 |
An array A contains n integers in locations A[0],A[1], …………… A[n-1]. It is required to shift the elements of the array cyclically to the left by K places, where 1≤K≤n-1. An incomplete algorithm for doing this in linear time, without using another is given below. Complete the algorithm by filling in the blanks. Assume all variables are suitably declared.
min:=n;
i=0;
while _____do
begin
temp:=A[i];
j:=i;
while _____do
begin
A[j]:=_____;
j:=(j+K) mod n;
if jA | Theory Explanation. |
Question 14 |
(a) Use the patterns given to prove that
(You are not permitted to employ induction)
(b) Use the result obtained in (a) to prove that
A | Theory Explanation. |
Question 15 |
Consider the following recursive function:
function fib (1:integer);integer; begin if (n=0) or (n=1) then fib:=1 else fib:=fib(n-1) + fib(n-2) end;
The above function is run on a computer with a stack of 64 bytes. Assuming that only return address and parameter and passed on the stack, and that an integer value and an address takes 2 bytes each, estimate the maximum value of n for which the stack will not overflow. Give reasons for your answer.
A | Theory Explanation. |
Question 16 |
An independent set in a graph is a subset of vertices such that no two vertices in the subset are connected by an edge. An incomplete scheme for a greedy algorithm to find a maximum independent set in a tree is given below:
V: Set of all vertices in the tree; I:=φ;
While V ≠ φdo
begin
select a vertex u; ∈ V such that
V:= V – {u};
if u is such that
then 1:= I ∪ {u}
end;
output(I);
(a) Complete the algorithm by specifying the property of vertex u in each case
(b) What is the time complexity of the algorithm.
A | Theory Explanation. |
Question 17 |
For parameters a and b, both of which are ω(1), T(n) = T(n1/a)+1, and T(b)=1.
Then T(n) is
A | θ(loga logb n) |
B | θ(logb loga n)
|
C | θ(log2 log2 n)
|
D | θ(logab n)
|
T(n) = [T(n1/a2)+1] + 1
= [T(n1/a3)+1] + 2
= [T(n1/a3)] + 3
= [T(n1/ak)] + b
= logb n = ak
= log logb n = k log a
= k= loga logb n
T(n)=1+loga logb n
T(n)=O(loga logb n)
Question 18 |
Let G = (V,E) be a weighted undirected graph and let T be a Minimum Spanning Tree (MST) of G maintained using adjacency lists. Suppose a new weighted edge (u,v) ∈ V×V is added to G. The worst case time complexity of determining if T is still an MST of the resultant graph is
A | θ(|E|+|V|) |
B | θ(|E| log|V|) |
C | θ(|E||V|) |
D | θ(|V|) |
• As T is a minimum spanning tree and we need to add a new edge to existing spanning tree.
• Later we need to check still T is a minimum spanning tree or not, So we need to check all vertices whether there is any cycle present after adding a new edge.
• All vertices need to traverse to confirm minimum spanning tree after adding new edge then time complexity is O(V).
Method-2:
Time Complexity:
Total vertices: V, Total Edges : E
• O(logV) – to extract each vertex from the queue. So for V vertices – O(VlogV)
• O(logV) – each time a new pair object with a new key value of a vertex and will be done at most once for each edge. So for total E edge – O(ElogV)
• So overall complexity: O(VlogV) + O(ElogV) = O((E+V)logV) = O(ElogV)
Note: Method-1 is the most appropriate answer for giving a question.
Question 19 |
Consider a graph G = (V, E), where V = {v1, v2, …, v100}, E = {(vi, vj) | 1 ≤ i < j ≤ 100}, and weight of the edge (vi, vj) is |i - j|. The weight of the minimum spanning tree of G is ______.
A | 99 |
• N =100
• Edge weight is |i-j| for Edge (vi,vj) {1<=i<=100}
• The weight of edge(v1,v2) is 1 , edge(v5,v6) is 1.
• So, 99 edges of weight is 99.
Question 20 |
Which of the following is false?
A | ![]() |
B | ![]() |
C | ![]() |
D | ![]() |

Question 21 |
The recurrence relation
T(1) = 2 T(n) = 3T(n/4)+n
has the solution, T(n) equals to
A | O(n) |
B | O(log n) |
C | O(n3/4) |
D | None of the above |
Question 22 |
The average number of key comparisons done on a successful sequential search in list of length n is
A | log n |
B | n-1/2 |
C | n/2 |
D | n+1/2 |
= No. of comparisons if element present in 1st position + No. of comparisons if element present in 2nd position + ............. + No. of comparisons if element present in nth position
= 1 + 2 + 3 + ... + n
= n(n+1)/2
Since there are n elements in the list, so average no. of comparisons
= Total comparisons/Total no. of elements
= (n(n+1)/2)/n
= n+1/2
Question 23 |
Quicksort is run on two inputs shown below to sort in ascending order taking first element as pivot,
(i) 1,2,3,...,n (ii) n,n-1,n-2,...,2,1
Let C1 and C2 be the number of comparisons made for the inputs (i) and (ii) respectively. Then,
A | C1 < C2 |
B | C1 > C2 |
C | C1 = C2 |
D | we cannot say anything for arbitrary n. |
So, option is (C) is correct.
Question 24 |
A two dimensional array A[1...n][1...n] of integers is partially sorted if
∀i, j ∈ [1...n−1], A[i][j] < A[i][j+1] and
A[i][j] < A[i+1][j]
Fill in the blanks:
(a) The smallest item in the array is at A[i][j] where i=............and j=..............
(b) The smallest item is deleted. Complete the following O(n) procedure to insert item x (which is guaranteed to be smaller than any item in the last row or column) still keeping A partially sorted.
procedure insert (x: integer);
var i,j: integer;
begin
(1) i:=1; j:=1, A[i][j]:=x;
(2) while (x > ...... or x > ......) do
(3) if A[i+1][j] < A[i][j] ......... then begin
(4) A[i][j]:=A[i+1][j]; i:=i+1;
(5) end
(6) else begin
(7) ............
(8) end
(9) A[i][j]:= .............
end
A | Theory Explanation. |
Question 25 |
Insert the characters of the string K R P C S N Y T J M into a hash table of size 10.
Use the hash function
h(x) = (ord(x) – ord("a") + 1) mod10
and linear probing to resolve collisions.
(a) Which insertions cause collisions?
(b) Display the final hash table.
A | Theory Explanation. |
Question 26 |
A complete, undirected, weighted graph G is given on the vertex {0, 1,...., n−1} for any fixed ‘n’. Draw the minimum spanning tree of G if
(a) the weight of the edge (u,v) is ∣u − v∣
(b) the weight of the edge (u,v) is u + v
A | Theory Explanation. |
Question 27 |
Let G be the directed, weighted graph shown in below figure.
We are interested in the shortest paths from A.
(a) Output the sequence of vertices identified by the Dijkstra’s algorithm for single source shortest path when the algorithm is started at node A.
(b) Write down sequence of vertices in the shortest path from A to E.
(c) What is the cost of the shortest path from A to E?
A | Theory Explanation. |
Question 28 |
Consider the following program that attempts to locate an element x in a sorted array a[] using binary search. Assume N>1. The program is erroneous. Under what conditions does the program fail?
var i,j,k: integer; x: integer;
a:= array; [1...N] of integer;
begin i:= 1; j:= N;
repeat k:(i+j) div 2;
if a[k] < x then i:= k
else j:= k
until (a[k] = x) or (i >= j);
if (a[k] = x) then
writeln ('x is in the array')
else
writeln ('x is not in the array')
end; A | Theory Explanation. |
Question 29 |
The correct matching for the following pairs is
(A) All pairs shortest path (1) Greedy (B) Quick Sort (2) Depth-First search (C) Minimum weight spanning tree (3) Dynamic Programming (D) Connected Components (4) Divide and and Conquer
A | A – 2 B – 4 C – 1 D – 3 |
B | A – 3 B – 4 C – 1 D – 2 |
C | A – 3 B – 4 C – 2 D – 1 |
D | A – 4 B – 1 C – 2 D – 3 |
Quick sort - Divide and Conquer
Minimum weight Spanning tree - Greedy
Connected components - Depth-First search
Question 30 |
Let T(n) be the function defined by T(1)= 1, T(n)= 2T(⌊n/2⌋) + √n for n≥2. Which of the following statement(s) is true?
A | T(n) = O(√n) |
B | T(n) = O(n) |
C | T(n) = O(log n) |
D | None of the above |
Question 31 |
Consider the following functions from positives integers to real numbers
The CORRECT arrangement of the above functions in increasing order of asymptotic complexity is:
A | ![]() |
B | ![]() |
C | ![]() |
D | ![]() |
Step-1: Take n=2048 or 211 (Always take n is very big number)
Step-2: Divide functions into 2 ways
1. Polynomial functions
2. Exponential functions
Step-3: The above functions are belongs to polynomial. So, simply substitute the value of n,
First compare with constant values.
→ 100 / 2048 = 0.048828125
→ 10 > 100/ 2048
→ log2 2048 =11
→ √n = 45.25483399593904156165403917471
→ n = 2048
So, Option B is correct
Question 32 |
Consider the following table
Match the algorithm to design paradigms they are based on:
A | (P)↔(ii), Q↔(iii), (R)↔(i) |
B | (P)↔(iii), Q↔(i), (R)↔(ii) |
C | (P)↔(ii), Q↔(i), (R)↔(iii) |
D | (P)↔(i), Q↔(ii), (R)↔(iii) |
(Q) QuickSort is a Divide and Conquer algorithm.
(R) Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem using Dynamic Programming.
Some important points regarding Greedy Vs Dynamic Programming
Greedy: →
It always gives polynomial time complexity
→ It is not an optimal
→ It always selects only either minimum or maximum among all possibilities
→ Ex: Dijkstra’s algorithm for SSSP, Optimal Merge Pattern, Huffman coding, Fractional knapsack problem, etc..,
Dynamic Programming:
→ It gives either polynomial or exponential time complexity.
→ It gives always an optimal result.
→ It checks all possibilities of a problem.
→ Ex: Longest Common sequence, Matrix chain Multiplication, Travelling sales Problem, etc.
Question 33 |
(I) Minimum Spanning Tree of G is always unique.
(II) Shortest path between any two vertices of G is always unique.
Which of the above statements is/are necessarily true?
A | (I) only |
B | (II) only |
C | both (I) and (II) |
D | neither (I) nor (II) |
Let us take an example

Step 1:
Using kruskal’s algorithm, arrange each weights in ascending order.
17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
Step 2:

Step 3:
17+18+20+21+22+23+26 = 147
Step 4:
Here, all the elements are distinct. So the possible MCST is 1.
Statement-II: May or may not happen, please take an example graph and try to solve it. This is not correct always.
So, we have to pick most appropriate answer.
Question 34 |
Let A be an array of 31 numbers consisting of a sequence of 0’s followed by a sequence of 1’s. The problem is to find the smallest index i such that A[i] is 1 by probing the minimum number of locations in A. The worst case number of probes performed by an optimal algorithm is _________.
A | 5 |
B | 6 |
C | 7 |
D | 8 |
→ As in this array sequence of 0’s is followed by sequence of 1’s, the array is sorted. We can apply binary search directly without sorting it.
So number of probes = ceil(log2 31) = 4.954196310386876
⇒ here we are using ceiling so it becomes 5
Question 35 |
Which one of the following algorithm design techniques is used in finding all pairs of shortest distances in a graph?
A | Dynamic programming |
B | Backtracking |
C | Greedy |
D | Divide and Conquer |
Question 36 |
Give the correct matching for the following pairs:
A. O(log n) 1. Selection sort
B. O(n) 2. Insertion sort
C. O(nlog n) 3. Binary search
D. O(n2) 4. Merge sort A | A – R B – P C – Q D – S |
B | A – R B – P C – S D – Q |
C | A – P B – R C – S D – Q |
D | A – P B – S C – R D – Q |
Selection = O(n)
Merge sort = O(n log n)
Insertion sort = O(n2)
Question 37 |
(a) Solve the following recurrence relation:
xn = 2xn-1 - 1, n>1 x1 = 2
(b) Consider the grammar
S → Aa | b A → Ac | Sd | ε
Construct an equivalent grammar with no left recursion and with minimum number of production rules.
A | Theory Explanation. |
Question 38 |
A sorting technique is called stable if
A | it takes O (nlog n) time |
B | it maintains the relative order of occurrence of non-distinct elements |
C | it uses divide and conquer paradigm |
D | it takes O(n) space |
Question 39 |
Suppose we want to arrange the n numbers stored in any array such that all negative values occur before all positive ones. Minimum number of exchanges required in the worst case is
A | n - 1 |
B | n |
C | n + 1 |
D | None of the above |
Now we will swap 1st no. with nth no. and then 2nd no. with (n-1)th no. and then 3rd no. with (n-2)th and so on. Like this we will have to do n/2 swaps in worst case.
Question 40 |
If one uses straight two-way merge sort algorithm to sort the following elements in ascending order:
20, 47, 15, 8, 9, 4, 40, 30, 12, 17
then the order of these elements after second pass of the algorithm is:
A | 8, 9, 15, 20, 47, 4, 12, 17, 30, 40 |
B | 8, 15, 20, 47, 4, 9, 30, 40, 12, 17 |
C | 15, 20, 47, 4, 8, 9, 12, 30, 40, 17 |
D | 4, 8, 9, 15, 20, 47, 12, 17, 30, 40 |

Question 41 |
If n is a power of 2, then the minimum number of multiplications needed to compute a* is
A | log2 n |
B | √n |
C | n-1 |
D | n |
We require 4 multiplications to calculate a16 .....(I)
→ Like that 3 multiplications requires to calculate a8 .....(II)
I, II are satisfied with the option A.
Question 42 |
The minimum number of record movements required to merge five files A (with 10 records), B (with 20 records), C (with 15 records), D (with 5 records) and E (with 25 records) is:
A | 165 |
B | 90 |
C | 75 |
D | 65 |
10, 20, 15, 5, 25
Merge 5 & 10:
5+10 = 15 movements
Now the list is
15, 20, 15, 25
Merge 15 & 15:
15+15 = 30 movements
Now the list is
30, 20, 25
Merge 20 & 25:
20+25 = 45 movements
Now the list is
30, 45
Merge 30 & 45:
30+45 = 75 movements
∴ Total no. of movements
= 15+30+45+75
= 165
Question 43 |
If T1 = O(1), give the correct matching for the following pairs:
(M) Tn = Tn−1 + n (U) Tn = O(n) (N) Tn = Tn/2 + n (V) Tn = O(nlogn) (O) Tn = Tn/2 + nlogn (W) T = O(n2) (P) Tn = Tn−1 + logn (X) Tn = O(log2n)
A | M – W N – V O – U P - X |
B | M – W N – U O – X P - V |
C | M – V N – W O – X P - U |
D | None of the above |
(N) Apply Master's theorem
T(n) = θ(n) = O(n)
(O) Apply Master's theorem
T(n) = θ(n logn) = O(n logn)
(P) Here we are adding the log of firstn natural numbers.
So,
Tn = log1 + log2 + log3 + ... + logn
= log (1×2×...n)
= log(n!)
= θ(n logn)
Question 44 |
Let A be an n×n matrix such that the elements in each row and each column are arranged in ascending order. Draw a decision tree which finds 1st, 2nd and 3rd smallest elements in minimum number of comparisons.
A | Theory Explanation. |
Question 45 |
(a) Consider the following algorithm. Assume procedure A and procedure B take O(1) and O(1/n) unit of time respectively. Derive the time complexity of the algorithm in O-notation.
algorithm what (n)
begin
if n = 1 then call A
else begin
what (n-1);
call B(n)
end
end.
(b) Write a constant time algorithm to insert a node with data D just before the node with address p of a singly linked list.
A | Theory Explanation. |
Question 46 |
Let s be a sorted array of n integers. Let t(n) denote the time taken for the most efficient algorithm to determined if there are two elements with sum less than 1000 in s. Which of the following statements is true?
A | t(n) is O(1) |
B | n ≤ t(n) ≤ n log2 n |
C | n log2 n ≤ t(n) < (n/2) |
D | t(n) = (n/2) |
Question 47 |
Consider the following functions
Which of the following is true?
A | h(n) is O (f(n)) |
B | h(n) is O (g(n)) |
C | g(n) is not O (f(n)) |
D | f(n) is O(g(n)) |
Then
f(n) = 3(n32) = 3*(210)32 = 3*2320
g(n) = 2320
h(n) = 1024!
So relation between the functions can be:
f(n) and g(n) are of same order, so f(n) is O(g(n)) and g(n) = O(f(n)). Option C is wrong.
h(n) is n! Which is of higher order than f(n) and g(n). So options A and B are wrong.
Question 48 |
Let G be an undirected connected graph with distinct edge weight. Let emax be the edge with maximum weight and emin the edge with minimum weight. Which of the following statements is false?
A | Every minimum spanning tree of G must contain emin |
B | If emax is in a minimum spanning tree, then its removal must disconnect G |
C | No minimum spanning tree contains emax |
D | G has a unique minimum spanning tree |

Minimum Spanning Tree:

Question 49 |
A recursive program to compute Fibonacci numbers is shown below. Assume you are also given an array f[0…..m] with all elements initialized to 0.
fib(n) {
if (n > M) error ();
if (n == 0) return 1;
if (n == 1) return 1;
if (▭) _________________(1)
return ▭ ____________(2)
t = fib(n – 1) + fib (n – 2);
▭__________(3)
return t;
}
(a) Fill in the boxes with expressions/statements to make fib() store and reuse computed Fibonacci values. Write the box number and the corresponding contents in your answer book.
(b) What is the time complexity of the resulting program when computing fib(n)?
A | Theory Explanation is given below. |
(2) f[n-2];
(3) f[n-2] = +;
(b) The time complexity of the resulting program when computing fib(n) is Θ(n).
Question 50 |
An array contains four occurrences of 0, five occurrences of 1, and three occurrences of 2 in any order. The array is to be sorted using swap operations (elements that are swapped need to be adjacent).
(a) What is the minimum number of swaps needed to sort such an array in the worst case?
(b) Give an ordering of elements in the above array so that the minimum number of swaps needed to sort the array is maximum.
A | Theory Explanation is given below. |
In Bubble sort maximum no. of swap is done when the elements are in non-increasing order, i.e.,
{2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0}
Pass 1 - 9 swaps
Pass 2 - 9 swaps
Pass 3 - 9 swaps
Pass 4 - 4 swaps
Pass 5 - 4 swaps
Pass 6 - 4 swaps
Pass 7 - 4 swaps
Pass 8 - 4 swaps
Pass 9 - 0 swaps
Pass 10 - 0 swaps
Pass 11 - 0 swaps
Total swaps = 47
(b) Same as part (a)
(a)

While traversing the tree we will get value,
E.val = 12
(b) While traversing the parse tree we will get 10 Reductions.










