Algorithms Test 3: Number of Questions: 25 Section Marks: 30
Algorithms Test 3: Number of Questions: 25 Section Marks: 30
a complete sub graph and if it does, then what is the for (int j = 1; j < n; j * = 2){
minimum value of ‘K’? for (int k = 0; k < n; k + = 2){
(A) 2 (B) 3 sum + = (i + j * k);
}
(C) 4 (D) None of the above
}
14. In the Build-Heap algorithm, the Heapify routine }
is invoked ‘n’ times. This indicates that Build-Heap (A) O(n3)
Complexity is _____. (B) O(n2 log n)
(A) O(log n) (B) O (n) (C) O(log n * log n * log n)
(C) O(n log n) (D) O(n2) (D) O(n * log n * log n)
15. Give asymptotically tight Big-O bounds for the follow- 22. Consider the modified binary search algorithm so that
ing recurrence relations. (By using Iterative substitu- it splits the input not into 2 sets of sizes equal sizes, but
tion method) into three sets of sizes approximately one-third. What
T(n) = T(n – 2) + 1 if n > 2 is the recurrence for this ternary search algorithm?
T(n) = 1 if n ≤ 2
(A) T(n) = T + T ( n − 2) + C
n
(A) O(n log n) (B) O(n) 2
(C) O(n2) (D) O(log n)
(B) T(n) = T + C
n
16. Consider the given code:
public static int f2(int n){ 3
(C) T(n) = T + T + C
int x = 0; 3n n
for (int i= 0; i < n; i++)
4 4
for (int j = 0; j < i * i; j++)
(D) T(n) = T + log n
x++; n
return x; 3
}
What is the order of growth of the above code? 23. Sort the following growth rate classes in increasing
(A) O(log n) (B) O(n2) order of time complexity:
3
(C) O(n ) (D) O(2n) Exponential, quadratic, logarithmic, cubic, and facto-
rial.
17. Consider the following:
(A) Logarithmic, quadratic, cubic, exponential, facto-
I. (3n)! = O(n!3) II. log (n!) = q(n log n)
rial
Which of the following is correct?
(B) Logarithmic, quadratic, cubic, factorial, exponen-
(A) I–False, II–False (B) I–False, II–TRUE
tial
(C) I–TRUE, II–False (D) I–TRUE, II–TRUE
(C) Quadratic, cubic, logarithmic, exponential, facto-
18. The Floyd-Warshall all pairs shortest path algorithm rial
for finding the shortest distances between nodes in a (D) Quadratic, cubic, logarithmic, factorial, exponen-
graph is an example of: tial
(A) An iterative based divide - and - conquer
24. Determine if ‘x’ is present in an array of n-elements:
(B) A Dynamic programming formulation
for i = 0 to n
(C) A greedy algorithm if (a[i] = x) return TRUE
(D) A recursive based divide - and - conquer else
19. Which of the following is not having O(n2) complexity? return false
(A) n + 2000n (B) n1.999 What is the worst case, best case time complexities and
n3 space complexity respectively?
(C) 106n + 26n (D)
n (A) O(n), O(1), O(n)
(B) O(n), O(1), O(1)
20. Consider the given two strings str1 and str2. (C) O(n), O(log n), O(1)
Let str1 = < A B R A C A D A B R O A > (D) O(log n), O(log n), O(n)
str2 = < Y A B B A D A B B A D O O B A >
Then the length of longest common subsequence would 25. What is the space complexity of following sorting algo-
be ___________. rithms respectively?
(A) 5 (B) 6 I. Quick sort II. Merge sort
(C) 7 (D) 8 III. Selection sort IV. Insertion sort
(A) O(n), O(n), O(1), O(1)
21. What is the computational complexity of the following (B) O(1), O(n), O(1), O(n)
piece of code: (C) O(1), O(n), O(1), O(1)
for (i = n; i > 0; i/= 2){
(D) O(1), O(n), O(n), O(1)
3.94 | Algorithms Test 3
Answer Keys
1. B 2. D 3. B 4. B 5. D 6. D 7. C 8. C 9. C 10. B
11. C 12. A 13. A 14. D 15. B 16. C 17. A 18. B 19. D 20. D
21. D 22. B 23. A 24. B 25. C
Statement III is not TRUE. Choice (B) (2) M [2, 4] = M[2, 3] + M[4, 4] + P1 P3 P4
k =3
2. Let M value be ‘3’
n We need to get the values of M[3, 4] and M[2, 3]
∑ 3i = 31 + 32 + 33... + 3n M [3, 4] = M[3, 3] + M[4, 4] + P2 P3 P4
k =3
i =1
⇒ ⇒ O ⇒ O(n)
n n
Choice (B)
2 2 str2 = Y A B B A D A B B A D O O B A
The longest common subsequence would be A B A D
16. Each iteration of the inner loop is quadratic in the outer
A B O A.
loop variable. The simplest way to do this is to realize
The length is ‘8’. Choice (D)
we are just summing Si2, which will just be O(n3), if we
use the integration trick. Choice (C) 21. In the outer for loop, the variable ‘i’ keeps halving, so it
goes a round (log n) times. For each ‘i’ next loop goes
17. Let us assume some value for n
round also (log n) times, because of doubling the vari-
n=5
n
I. (3 × 5)! = 15! = very large number able j. The inner most loop by ‘k’ goes round times.
(5!)3 = 1203 = 1728000 2
Loops are nested, so the bounds may be multiplied to
(3n)! < O(n!3)
give that the algorithm is O(n * log n * log n)
I–false
Choice (D)
II. log(n!) = q(n log n)
lets take n = 8 22. By analogy, we divide the elements instead of 2 sub-
log (n!) = log(8!) = log(40320) = 15.299 sets, we divide them into three subsets.
n log n = 24 n
\ T(n) = T + c Choice (B)
log (n!) < (n log n) 3
II–false Choice (A)
23. logarithmic → log n
18. The Floyd-warshall all pairs shortest path algorithm in cubic → n3
a graph is an example of a dynamic programming for- quadratic → n2
mulation. Exponential → 2n
Choice (B) Factorial → n!
19. Option (A) logn < n2 < n3 < 2n < n!
n + 2000n ≤ c * n2 Logarithmic, quadratic, cubic, exponential, Factorial.
⇒ n ≤ c * n2 (TRUE) Choice (A)
Option (B) 24. The procedure given in problem performs linear search,
n1.999 ≤ c * n2 (TRUE) Worst case ⇒ O(n)
Option (C) Best case ⇒ O(1)
106n + 26n ≤ c * n2 We do not require extra space to search for an element
n ≤ c * n2 (TRUE) in an array, so space complexity is O(1). Choice (B)
Option (D)
25. Quick sort, selection sort, insertion sort are ‘In-place’
n3
≤ c * n2 (False) algorithms means they do not take extra space.
n \ O(1), O(1), O(1)
n2.5 ≤ c * n2(False) Merge sort is Not In-Place algorithm.
\ for a very large value of ‘n’ It needs extra space
Choice (D) \ O(n). Choice (C)