You are on page 1of 4

Design and Analysis of Algorithms - Sample Questions

1. Is it possible to measure the efficiency of an algorithm which does not depend on the
state of current technology? If yes, how?
2. An algorithm is made up of two independent time complexities f (n) and g (n). Then the
complexities of the algorithm is in the order of
a. f(n) x g(n)
b. Max ( f(n),g(n))
c. Min (f(n),g(n))
d. f(n) + g(n)

3. Consider the following segment of code:


int j, n;
j = 1;
while ( j <= n)
j = j *2;
The number of comparisons made in the execution of the loop for any n > 0 is”
a. log 2 n + 1
b. n
c. log 2 n
d. log 2 n +1 [

4. Which algorithm has same average, worst case and best case time :
(A) Binary search (B) Maximum of n numbers
(C) Quick sort (D) Searching a singly linked list

5. Arrange the following in the increasing order of time complexity: O(n2), O(n log n),
O(2n), O(log n)

6. Which of these is the correct big-O expression for 1+2+3+...+n?


a. O(log n) b. O(n) c. O(n log n) d. O(n²)

Because this the summation of the first ‘n’ numbers, that is n(n+1)/2 => ( n2+n)/2 =>
then by ignoring the lower order terms and the constants this becomes n2.

7. Adding an element to an array that does not allow duplicates requires how many
comparisons in the worst case?

8. What is the significance of asymptotic notations? Show that the algorithm to find the
maximum from an array is θ(n)

10. What is the format of a recurrence relation? Solve the following recurrence relation using
Substitution method. T(n) = 4T(n/3) + n

Sensitivity: Internal & Restricted


A linear recurrence equation of degree k or order k is a recurrence equation which is in the
format xn=A1xn−1+A2xn−1+A3xn−1+…Akxn−k(An is a constant and Ak≠0) on a
sequence of numbers as a first-degree polynomial

11. If f(n) = am nm + ……+a1n+a0, then show that f(n)=O(nm)


Consider f(n) = 6 ∙ 2 n + n 2 .
f(n) = 6 ∙ 2 n + n 2 ≤ 6 ∙ 2 n + 2n = 7 ∙ 2 n ,
for all n ≥ 4 Therefore f(n) = O(2n )

12. b) Solve the following recurrence relation using Substitution method.


T(n) = 1 n=1
T(n) = 2T(n/2) + n n>1

1 Solving Recurrences with the Substitution Method


• Idea: Make a guess for the form of the solution and prove by induction.
• Can be used to prove both upper bounds O() and lower bounds Ω().
• Let’s solve T(n) = 2T(n/2) + n using substitution
– Guess T(n) ≤ cn log n for some constant c (that is, T(n) = O(n log n))
– Proof:
∗ Base case: we need to show that our guess holds for some base case (not necessarily
n = 1, some small n is ok). Ok, since function constant for small constant n.
∗ Assume holds for n/2: T(n/2) ≤ c n 2 log n 2 (Question: Why not n − 1?) Prove that
holds for n: T(n) ≤ cn log n

T(n) = 2T(n/2) + n ≤ 2(c n 2 log n 2 ) + n = cn log n 2 + n = cn log n − cn log 2 + n = cn


log n − cn + n So ok if c ≥ 1 • Similarly it can be shown that T(n) = Ω(n log n) Exercise!
• Similarly it can be shown that T(n) = T(b n 2 c) + T(d n 2 e) + n is Θ(n lg n). Exercise! •
The hard part of the substitution method is often t
13.

14. Write the control abstraction for Divide and Conquer Strategy.
In divide and conquer approach, a problem is divided into smaller problems, then the
smaller problems are solved independently, and finally the solutions of smaller problems
are combined into a solution for the large problem.

Generally, divide-and-conquer algorithms have three parts −

Divide the problem into a number of sub-problems that are smaller instances of the same
problem.

Conquer the sub-problems by solving them recursively. If they are small enough, solve the
sub-problems as base cases.

Combine the solutions to the sub-problems into the solution for the original problem.

15. What is the lower bound on a comparison based sorting method?

Sensitivity: Internal & Restricted


16. In selecting the pivot for quicksort, which is the best choice is for optimal partitioning?
Justify your answer

17. In what manner will you arrange the following elements that if the quick sort is
performed on this new sequence, the sorting time becomes maximum i.e. of order O(n2)
where n represents the number of elements in the sequence. Justify your answer.
12, 5, 7, 10, 17, 18, 23, 8, 6, 13

18. The lower bound for comparison based sorting methods is O(n lg n). Is it possible to sort
faster than O(n lg n)? Justify your answer.

19. Consider the following program segment. What is the Space complexity?
Begin
i) i=0;
ii) S=0;
iii) S=s+1;
iv) Return s;
End

20. What is the time complexity of optimal binary search


(1) a)O(n) b) O(1) c)O(2n/2) d) O(n2)

21. Randomised algorithms are also called as __________________ and whose behavior is
dependent on _______________ in decision making as part of its logic

22. Match the following

Problem Recurrence Equation


1 Binary Search A tn – tn-1 = 1
2 Merge Sort B T(n)=2T(n/2)+n-1
3 Sequential Search C tn = tn-1 + n
4 Factorial D T(n)=7T(n/2)+18(n/2)2
5 Strassen Matrix Multiplication E T(n)=T(n/2)+1
6 Selection Sort F T(n)=T(n/2)+n-1

23. Use the Master method to find T(n) = Θ(?) in each of the following cases:
a) T(n) = 7T(n/2) + n2
b) T(n) = 4T(n/2) + n2
c) T(n) = 2T(n/3) + n3

24. Fibonacci series is defined as follows:


f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2)

Sensitivity: Internal & Restricted


Find an iterative algorithm and a recursive one for computing element number n in
Fibonacci series, Fibonacci(n). Analyze the running-time of each algorithm.

25. Apply merge sort for the following list of elements: 6, 3, 7, 8, 2, 4, 5, 1 . Analyze the
time complexity of Merge Sort

26. Consider the following four matrices whose orders are given and perform chain matrix
multiplication using the dynamic programming approach
Matrix A1 A2 A3 A4
size 2x2 2x3 3x4 4x2

27. S = {A, B, C, B} , T = {B, D, C, A} Find the Longest Common Subsequence of


S and T with detailed procedure
28. Obtain the optimal parenthesization for the multiplication of the following four matrices
such that the number of scalar multiplications is minimum.
A1 ( 5X2) A2(2X10) A3(10X3) A4(3X2)
29. Fill in the following table for the times to sort an array of n items. Use only big-O
notation, and do not have any extraneous constants in your expressions.
Algorithms Worst Case AverageCase
Selection sort
Insertion sort
Quick sor
Merge sort
Binary search of a sorted array
30. Give the complexity in terms of Big O notation for the following algorithmic equations ?
(a) 5.n2 – 6n + 2 (b)6.2n + 6
31. Give the analysis of the complexity of the divide and conquer strategy.
32. Sort the given values using Quick Sort. Analyze complexity of Quick sort

65 70 75 80 85 60 55 50 45

33. Solve T(n) = 3T(n/4) + cn2 using recursion-tree method and Substitution method
34.

Sensitivity: Internal & Restricted

You might also like