0% found this document useful (0 votes)
48 views6 pages

Analysis of Algorithms (SET: 2) : Q.1 What Is Time Complexity of Fun ?

This document contains 20 multiple choice questions about time complexity analysis of algorithms. The questions cover topics like analyzing time complexity of functions, order of asymptotic complexity, loop invariants, recursion, and more.

Uploaded by

sanskriti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views6 pages

Analysis of Algorithms (SET: 2) : Q.1 What Is Time Complexity of Fun ?

This document contains 20 multiple choice questions about time complexity analysis of algorithms. The questions cover topics like analyzing time complexity of functions, order of asymptotic complexity, loop invariants, recursion, and more.

Uploaded by

sanskriti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ANALYSIS OF ALGORITHMS

(SET: 2)

Q.1 What is time complexity of fun( )?

int fun(int n)
{
int count=0;
for (int i= n; i> 0; i/=2)
for(int j=0; j< i; j++)
count+= 1;
return count;
}

(a) O(n^2) (b) O(nlog n)


(c) O(n) (d) O(nlog n log n)

Q.2 What is the time complexity of fun( )?

int fun(int n)
{
int count=0;
for(int i=0; i<n; i++)
for(int j=I; j>0; j--)
count= count+1;
return count;
}

(a) Theta(n) (b) Theta(n^2)


(c) Theta(n*log n) (d) Theta(nlog n log n)

Q.3 The recurrence relation capturing the optimal time of the Tower of Hanoi problem with n
discs is—

(a) T(n) = 2T(n – 2) + 2 (b) T(n) = 2T(n – 1) + n


(c) T(n) = 2T(n/2) + 1 (d) T(n) = 2T(n – 1) + 1

1
Q.4 Let w(n) and A(n) denote respectively, the worst case and average case running time of an
algorithm executed on an input of size n. which of the following is ALWAYS TRUE?
(GATE CS 2012)

(a) A(n)=Ω(n) (b) A(n)=theta(n)


(c) A(n)=O(n) (d) A(n)=o(n)

Q.5 Which of the following is not O(n^2)?

(a) (15^10) * n + 12099 (b) n^1.98


(c) n^3 / (sqrt(n)) (d) (2^20) * n

Q.6 Which of the given options provides the increasing order of asymptotic complexity of
functions f1, f2, f3 and f4?

f1(n) = 2^n
f2(n) = n^(3/2)
f3(n) = nLogn
f4(n) = n^(Logn)

(a) f3, f2, f4, f1 (b) f3, f2, f1, f4


(c) f2, f3, f1, f4 (d) f2, f3, f4, f1

Q.7 Consider the following program fragment for reversing the digits in a given integer to obtain
a new integer. Let n = D1D2…Dm

int n, rev;
rev = 0;
while (n > 0)
{
rev = rev*10 + n%10;
n = n/10;
}

The loop invariant condition at the end of the ith iteration is: (GATE CS 2004)
(a) n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1
(b) n = Dm-i+1…Dm-1Dm and rev = Dm-1….D2D1
(c) n!= rev
(d) n = D1D2….Dm and rev = DmDm-1…D2D1

2
Q.8 What is the time complexity of the below function?

void fun(int n, int arr[ ])


{
int i = 0, j = 0;
for(; i < n; ++i)
while(j < n && arr[i] < arr[j])
j++;
}

(a) O(n) (b) O(n^2)


(c) O(nlog n) (d) O(n(log n) ^2)

Q.9 In a competition, four different functions are observed. All the functions use a single for
loop and within the for loop, same set of statements are executed. Consider the following for
loops:

(A) for(i = 0; i < n; i++) (B) for(i = 0; i < n; i += 2)


(C) for(i = 1; i < n; i *= 2) (D) for(i = n; i > -1; i /= 2)

If n is the size of input (positive), which function is most efficient (if the task to be performed is
not an issue)?
(a) A (b) B
(c) C (d) D

Q.10 What does it mean when we say that an algorithm X is asymptotically more efficient than
Y?

(a) X will be a better choice for all inputs


(b) X will be a better choice for all inputs except small inputs
(c) X will be a better choice for all inputs except large inputs
(d) Y will be a better choice for small inputs

Q.11 What is the time complexity of Floyd–Warshall algorithm to calculate all pair shortest path
in a graph with n vertices?

(a) O(n^2logn) (b) Theta(n^2logn)


(c) Theta(n^4) (d) Theta(n^3)

3
Q.12 Consider the following functions:
f(n) = 2^n
g(n) = n!
h(n) = n^logn

Which of the following statements about the asymptotic behavior of f(n), g(n), and h(n) is true?
(a) f(n) = O(g(n)); g(n) = O(h(n)) (b) f(n) = Ω (g(n)); g(n) = O(h(n))
(c) g(n) = O(f(n)); h(n) = O(f(n)) (d) h(n) = O(f(n)); g(n) = Ω (f(n))

Q.13 The minimum number of comparisons required to find the minimum and the maximum of
100 numbers is _________________.

(a) 147.1 to 148.1 (b) 145.1 to 146.1


(c) 140 to 146 (d) 140 to 148

Q.14 In the following C function, let n >= m.

int gcd(n,m)
{
if (n%m ==0) return m;
n = n%m;
return gcd(m, n);
}

How many recursive calls are made by this function?


(a) theta(log n) (b) Ω(n)
(c) theta(log log n) (d) theta(sqrt(n))

Q.15 Consider the following functions:

f(n) = 3n√x
g(n) = 2√x log2 n
h(n) = n!

Which of the following is true? (GATE CS 2000)


(a) h(n) is O(f(n)) (a) h(n) is O(f(n))
(c) g(n) is not O(f(n)) (d) f(n) is 0(g(n))

4
Q.16 Consider the following three claims:

I) (n + k)^m = (n^m), where k and m are constants


II) 2^(n + 1) = 0(2^n)
III) 2^(2n + 1) = 0(2^n)

Which of these claims are correct? (GATE CS 2003)


(a) I and II (b) I and III
(c) II and III (d) I, II and III

Q.17 int unknown (int n) {


int i, j, k = 0;
for (i = n/2; i <= n; i++)
for (j = 2; j <= n; j = j * 2)
k = k + n/2;
return k;
}

What is the returned value of the above function? (GATE CS 2013)


(a) Ѳ(n2) (b) Ѳ(n2 log n)
(c) Ѳ(n3) (d) Ѳ(n3 log n)

Q.18 Consider the following two functions. What are time complexities of the functions?

int fun1(int n)
{
if (n <= 1) return n;
return 2*fun1(n-1);
}

int fun2(int n)
{
if (n <= 1) return n;
return fun2(n-1) + fun2(n-1);
}

(a) O(2^n) for both fun1() and fun2() (b) O(n) for fun1() and O(2^n) for fun2()
(c) O(2^n) for fun1() and O(n) for fun2() (d) O(n) for both fun1() and fun2()

5
Q.19 Consider the following segment of C-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:
Base of Log is 2 in all options.
(a) CEIL(logn) + 1 (b) n
(c) CEIL(logn) (d) FLOOR(logn) + 1

Q.20 Consider the following C-program fragment in which i, j and n are integer variables.
for (i = n, j = 0; i >0; i /= 2, j += i);

Let val(j) denote the value stored in the variable j after termination of the for loop. Which one of
the following is true?
(a) val(j) = theta(log n) (b) val(j) = theta(sqrt(n))
(c) val(j) = theta(n) (d) val(j) = theta(nlog n)

You might also like