Answer: A Sol. Big-Oh Notation Represents The Worst Case - So Highest Magnitude Among The Function Represent

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Q1.Let A be an array of n integers, sorted, so that A[1]<A[2]<A[3]< .…A[n].

Suppose you are


given a number and you wish to find out if there are indices A[p]and A[q] such that A[p] +
A[q]=x . What will be the time complexity

(a)n (b) nlogn (c) n^2logn (d) log(log(n))

Answer: a

Sol. As the given array is sorted, required elements will be find in O(n) time

If A[p]+A[q] > x then q-- else we increment p++

Hence , required element can be found in O(n) time.

Q2. Which of the following statement is/are correct:

1.If f(n)=O(g(n)) => g(n) = O(f(n))

2.If f(n)= O(g(n)) and g(n) = O(h(n)) then f(n)=O(h(n))

3.If f(n)=O(g(n)) => g(n) = Omega(f(n))

(a) 2 only (b) 3 only (c) 2 and 3 only (d) All of the above

Answer: c

Sol. 1. Big-Oh function is the upperbound of the fuction i.e g(n) is upper bound of f(n). So, it
doesn’t follow symmetric property.

2. In this expression function follows transitivity property.

3. As g(n) is the worst case for f(n). So, f(n) is the best case of g(n). It follows transpose
property.

Q3. There are four non-negative functions are given as f(n) =O(d(n)) and g(n)= O(e(n)). Then
which of the following expression is correct

(a) f(n)+ g(n) =max (d(n), e(n))


(b) f(n)+ g(n) =min (d(n), e(n))
(c) f(n).g(n) =max (d(n), e(n))
(d) f(n). g(n) =min (d(n), e(n))

Answer: a

Sol. Big-Oh notation represents the worst case . So highest magnitude among the function represent
the complexity
f(n)+ g(n) =max (d(n), e(n)) Highest magnitude of the two function in addition.

f(n).g(n)=max(d(n),e(n)) In multiplication operation highest magnitude after the


multiplication is answer.

Q4. Match the following for the correct pair

A. Kruskal Algorithm (p) Dynamic Programming


B. Quick Sort (q) Backtracking
C. Hamiltonian Circuit (r) Greedy Method
D. All Pair Shortest Path (s) Divide and Conquer

A B C D
(a) p r q s
(b) r s p q
(c) r s q p
(d) p s r q

Answer: c

Sol.

 Kruskal is greedy about the minimum weight in each iteration and used for MST
construction.
 In Quick Sort we use divide and conquer paradigm
 In Hamiltonian Circuit we use backtracking
 All pair shortest path use Dynamic Programming paradigm (Floyd Warshall algorithm )

Q5. What is the asymptotically correct order of the following

1. log(log^a n)
2. log(n!)
3. log^a(n!)
4. log^a(logn)

(a) 1<2<3<4 (b) ) 1<4<2<3 (c) 2<3<4<1 (d) 3<4<2<1

Answer: b

Sol.
Q6. Given a set of m = 2^n distinct numbers, we would like to determine the smallest
and the second smallest using comparisons. Which of the following statements is TRUE?

(a) Both the elements can be determined using 2n comparisons.


(b) Both the elements can be determined using m-2 comparisons.
(c) Both the elements can be determined using 2m, comparisons.
(d) Both these elements can be determined using m+n-2 comparisons.

Answer: d

Sol.

Q7. Consider a plate stacked with several disks, each of a different diameter (they could
all be, for instance, chapatis or parantha of different sizes). We want to sort these disks
in decreasing order according to their diameter so that the widest disk is at the bottom of
the pile. The only operation available for manipulating the disks is to pick up a stack of them
from the top of the pile and invert that stack. (This corresponds to lifting up a stack, chapatis
or parantha between two big spoons and flipping the stack.)

(a) n^2 (b) n^3 (c) 2^n (d) nlogn

Answer: c

Sol. Here we will use the Tower of Hanoi approach,


So the answer will be 2^n.

Q8. There are n lists are given with m elements each sorted in non-decreasing order. Merging
these lists into a single sorted list will take time.

(a) O(nm log m)


(b) O(mn log n)
(c) O(m + n)
(d) O(mn)

Answer : b

Sol. Since, n lists of each size m .

Since, each list is sorted in ascending order use directly merge procedure of merge sort algo.

Take two list and merge..so one pair will take 2m time.

So, total pairs in first level will be n/2 . So total cost for one level is (n/2)*2m=nm.
In next level cost for one pair is 4m and no of pairs will be n/4 .. so next level cost will be nm .

So, like this each level will have cost nm .

No of levels will be when we have one complete list..

n/2^k=1..

k= logn(base 2)

So , total cost will be O(mn logn).

Q9. What will be the time complexity for the following pseudo code:

Sum = 0;

for( i=1; i<=n;i++)

{ for (i=1; j<=n; j++)

{ Sum = Sum + j;}

(a) O(n) (b) O(nlogn) (c)n^2 (d) n^2logn

Answer: c

Sol. Sum = 0;

for( i=1; i<=n;i++) ------------(this will run n times )

{ for (i=1; j<=n; j++)----------(for every value of i this will run n times )

{ Sum = Sum + j;}

So,for i=1, j will run n times

i=2, j will run n times……

i=n, j will run n times


Hence, n+n+n+……………n,

n^2.

You might also like