Professional Documents
Culture Documents
SOLUTIONS
int a, b, i;
int k = 0;
int n = 32;
for (a = 1; a ≤ n; a = 2 * a)
{
for (b = 1; b ≤ n; b = b + 10)
{
for (i = 1; i ≤ n; i = 2 * i)
{
k = k+1;
}
}
}
printf(“%d”, k);
(a) 80 (b) 81
(c) 132 (d) 144
Explanation:
(d)
The value of ‘k’ printed is equal to the number of times “k = k+1” is executed.
1
(a) O(h(n)) (b) Θ(h(n))
(c) Ω(h(n)) (d) None of these
Explanation:
(b)
It is given that
f(n) = Θ (g(n))
f(n) = c1·g(n)
It is also given that g(n) = Θ(h(n))
g(n) = c2·h(n)
f(n) = c1·c2h·(n)
f(n) = c3·h(n)
f(n) = Θ (h(n))
3. Consider the following recurrence relation to find greatest common divisor of two numbers P
and Q.
Explanation:
(b)
GCD algorithm has worst case time complexity when both P and Q are prime numbers.
The worst case time complexity is O(log2 max(Q, P)).
2
4. Which of the following statements is/are true?
Explanation:
(c)
In heap sorting algorithm we extract the elements one by one. First the smallest (or largest) item
is located and it is separated from the rest, then the next smallest (or next largest) is selected and
so on until all items are separated.
5. Using Kruskal’s algorithm how many distinct minimum cost spanning trees are possible?
(a) 3 (b) 4
(c) 5 (d) 6
Explanation:
3
(b)
6. Alice needs to send a large message to Bob using only five words. Seeing that the message is
too long she decides to compress the message using Huffman coding algorithm.
If the respective frequencies of words are given in the table, what would be the hamming code
for w3w1w2 that she was supposed to use?
Explanation:
(b)
The following are hamming codes obtained by using Huffman coding algorithm:
w4 = 0 w3 = 1000
w1 = 11 w5 = 1001
w2 = 101
∴ w3w1w2 = 100011101
4
Identify the respective expressions for A, B and C.
Explanation:
(d)
8. How many passes of bubble sort are required to sort the following sequence (Pass is counted
only when at least one swap is performed in the bubble sort pass)?
(a) 6 (b) 5
(c) 4 (d) 3
Explanation:
(c)
9. Consider an array A of size K where each element is either 10 or 20. What is the minimum
time required to sort the array A.
5
(a) O(K log K) (b) O(K2)
(c) O(log K) (d) O(K)
Explanation:
(d)
Take two variables ‘i’ and ‘j’ such that i = 0 and j = K – 1. ‘i’ moves right until it finds 20 and ‘j’
moves left until it finds ‘10’. Then both will swap, and procedure continues as long as i < j. In
this way maximum ‘K’ comparisons are required.
10. A graph G is such that every vertex is connected to every other vertex via direct link. We
need to find shortest path between two vertices using Bellman ford algorithm. How much time
does it take?
Explanation:
(c)
In Bellman ford algorithm for every round ‘E’ edges are relaxed. Each edge relaxation takes
constant time. In general there are (V – 1) rounds excluding the verification round.
11. Given a hash table with 6 keys and 10 slots, with simple uniform hashing. If collisions are
resolved by chaining then the probability that first slot ends up empty?
Explanation:
6
(0.531) [0.53 – 0.54]
1 6 9 6
(1 − ) = ( ) = 0.531
10 10
12. A hash table of size 11 using the hash function h(x) = x mod 11 and Quadratic probing with
hi(x) = (h(x)+i2) mod 11 ∀i ∈ {0, 1, 2, … 10}.
The key values are given in the following order: 41, 27, 9, 21, 13, 22, 23, 26, 2, 30. The location
of ‘30’ in the hash table, if the index value starts from ‘0’ is __________.
Solution: 6
Explanation:
(6)
∴ 30 is located at 6.
13. Assume Dijkstra’s algorithm is used to find the shortest paths from node ‘A’ in the following
graph.
The number of edges are there which are not included in any of the shortest paths from node A is
___________.
Solution: 4
7
Explanation:
(4)
A → E : 3[edge e1]
A → D : 6[edge e3]
A → B : 4[edge e2]
A → C : 9[edge e2, edge e5]
The edges e4, e6, e7 and e8 are never used in any of the shortest paths.
Solution: 71
Explanation:
71
P1 P2 P3 P7
( , , , … , ) = (4, 2, 3.33, 1.142, 5, 6, 2.5)
w1 w2 w3 w7
P
Decreasing order of wi is x6 , x5 , x1 , x3 , x7 , x2 , x4
i
Pi
Since capacity of bag is 17 we can only add the following items using the ratio.
wi
x6 (4) + x5 (2) + x1 (3) + x3 (6) + x7 (2)
Total profit = 1 × 24 + 1 × 10 + 1 × 12 + 1 × 20 + 1 × 5
= 24 + 10 + 12 + 20 + 5
= 71 units.
15. Consider a set of 20 elements. To find maximum and minimum element in the given set, the
minimum number of comparisons required is ________?
(using DAC Max-Min algorithm)
8
Solution: 28
Explanation:
28
3
The minimum comparisons required are 2 n − 2
3(20)
∴ − 2 = 28 comparisons
2
16. A newly introduced machine can sort 400 entries in 400 sec using bubble sort. The number
of entries the same machine sort in 1600 sec using bubble sort is __________.
Solution: 800
Explanation:
(800)
Explanation:
(c)
9
Quick sort is not stable algorithm i.e. if the elements are repeated in the array, their order may or
may not be preserved after the application of quick sort algorithm. Hence S2 is false.
1
18. Let f1 = 100000n, f2 = 30 n2 , f3 = (log n)200 , f4 = 2n and f5 = n log n. Which of the
following is the correct order of growth in ascending order.
(a) f3, f1, f2, f5, f4 (b) f3, f2, f1, f4, f5
(c) f3, f1, f5, f2, f4 (d) f3, f2, f4, f1, f5
Explanation:
(c)
I. 2n2 = O(n2)
II. 2n = O(n2)
III. n2 = O(n5)
IV. n4 = Ω(n3)
Explanation:
(a)
II, III and IV are not asymptotically tight. They can be expressed with more tightness.
2n = o(n2)
n2 = o(n5)
n4 = ⍵(n3)
20. Consider the following recurrence relation for the running time of strassen’s algorithm.
10
Θ(1) if n = 1
T(n) = { n
PT ( ) + Θ(nR )
Q
What are the respective values of P, Q and R?
(a) 2, 7, 3 (b) 7, 2, 3
(c) 7, 2, 2.81 (d) 7, 2, 2
Explanation:
(d)
Θ(1) if n = 1
T(n) = { n
7T (2) + Θ(n2 )
T(n) = 2T(n⁄4) + √n + 42
Explanation:
(a)
22. Given that X = 〈A, B, C, B, D, A, B〉 and Y = 〈B, D, C, A, B, A〉. Which of the following is true?
11
(a) 〈B, C, A〉 is the longest common subsequence (LCS) of X and Y
(b) 〈B, C, B, A〉 is one of the LCS of X and Y
(c) LCS of length 5 or more exists
(d) None of these
Explanation:
(b)
X and Y have LCS’s of length 4. The following are longest common subsequences of X and Y.
〈B, C, B, A〉
〈B, D, A, B〉
23. Assume that enqueing and dequeing operation take O(V) time each, during BFS algorithm on
a graph. What would be the worst case time complexity of BFS algorithm? (V is the number of
vertices in the graph and graph is given as input to BFA algorithm in the form of adjacency list).
Explanation:
(c)
In normal BFS both enqueing and dequeing operation takes O(1) but in this algorithm it takes
O(V) time.
Adjacency list is scanned for every vertex, when it is dequed. So overall O(E) time to scan
adjacency lists
∴ O(V2 + E) = O(V2)
12
S1: Dijkstra’s algorithm is not affected by negative edge weight cycles in the graph and gives
correct shortest path.
S2: Bellman ford algorithm finds all negative edge weight cycles present in the graph.
Explanation:
(d)
Dijkstra’s algorithm assumes that all edge weights are non-negative. Because of this negative
edge weight cycles are undetected. Hence Dijkstra’s algorithm is bound to give wrong shortest
path whenever there is a negative weight cycle. It finds only negative edge weight cycle which
are reachable from source.
Bellman Ford algorithm detects the existence of negative cycle, but it does not name the nodes in
the cycle explicitly. Also, Bellman Ford algorithm finds the shortest path even if there are
negative edge weights but not if there are negative cycles.
25. Consider hashing with chaining as collision resolution technique is implemented. The load
factor obtained was 3. What does it indicate?
Explanation:
(c)
13
26. What is the minimum of the multiplications required to multiply the following three matrices.
(a) 96 (b) 97
(c) 98 (d) 378
Explanation:
(a)
27. A hash table of size 10, is shown in the figure with symbols stored from a to g using some
hash function with linear probing.
The worst case number of comparisons required when the symbol being searched is not in the
table is ?
14
(a) 5 (b) 4
(c) 3 (d) 7
Explanation:
(b)
The search using linear probing stops whenever it finds an empty slot.
28. Find the total possible number of back edges that we come across during depth first traversal
on the following directed graph. Algorithm starts from vertex A (back edges are those edges
connecting a vertex u to an ancestor V in a depth first tree).
(a) 3 (b) 4
(c) 5 (d) 6
Explanation:
(c)
Back edges are those edges connecting a vertex u to an ancestor V in a depth first tree.
15
There are only 5 back edges.
29. Consider an array with the following elements: 12, 18, 17, 11, 13, 15, 16 and 14.
How many elements will change their initial position after completion of partition algorithm by
choosing 15 as pivot?
Solution: 7
Explanation:
(7)
30. Using Kruskal’s algorithm, the weight of the minimal spanning tree for the following graph
is ___________.
Solution: 66
16
Explanation:
(66)
∴ Weight = 13 + 16 + 11 + 14 + 12 = 66
Which key in the above sequence is making the search sequence invalid, if the search was made
for 110?
Solution: 220
Explanation:
(220)
The search key 110 is less than 190. Therefore key 220 to the right of 190 makes this search
sequence invalid.
32. Consider the following min heap tree. The number of element comparisons required to
maintain the heap property after deletion of root are __________.
Solution: 6
17
Explanation:
(6)
During deletion, the element is replaced by last element (196 in this case) in the last level. We
need to compare it with two of it’s children values. Parent will be compared to minimum of
(Lchild, Rchild). 196 will be compared and swapped with min (120, 130) and so on…. This will
result in 6 comparisons.
33. We are given jobs J1, J2, … , J8 and each job requires one unit of execution. Only one job can
be executed at a time. Each job has its respective profit and deadlines as shown in the table
below.
Solution: 122
Explanation:
(122)
The following table takes the completion of tasks according to their deadlines to obtain
maximum profit.
Profit = 122
18