0% found this document useful (0 votes)
4 views

Algorithms Test Paper -1

This document is a weekly test for a computer science course focusing on algorithms and design strategies, consisting of multiple-choice questions (MCQs) and multiple-select questions (MSQs). It includes questions on time complexity, data structures, and algorithm efficiency, along with an answer key and hints for solutions. The test is designed for students in the CSE/IT branch, with a maximum score of 15 marks.

Uploaded by

Sachin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Algorithms Test Paper -1

This document is a weekly test for a computer science course focusing on algorithms and design strategies, consisting of multiple-choice questions (MCQs) and multiple-select questions (MSQs). It includes questions on time complexity, data structures, and algorithm efficiency, along with an answer key and hints for solutions. The test is designed for students in the CSE/IT branch, with a maximum score of 15 marks.

Uploaded by

Sachin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Branch : CSE/IT Batch : Hinglish


WEEKLY TEST – 02
Subject : Algorithms
Topic : Design Strategies
Maximum Marks 15

Q.1 to 5 Carry ONE Mark Each


[MCQ] [MSQ]
1. What is the tightest Big -O time complexity for the 3. Consider the following statements
following code? (a) Inserting, Deleting in arrays takes O(N) time,
int fun(int n) containing N elements.
{ (b) Inserting, deleting in string takes O(N) time.
if (n < = 0) (c) Space complexity for each operation in a linked
{ list is O(1)
return 0; (d) The worst-case time complexity of binary search
} tree is O(n).
else if (n> = 2000)
{ [MCQ]
return 1; 4. What will be the worst case time complexity for the
following code?
}
int foo(int a[ ], int x)
int count = 0 {
for (int a = 0; a <n; a++) int result = 0;
{ for (int p = 0; p < x; p++){
count ++; for (int q = 0; q < 7; q++){
} for (int r = 0; r < x; r++){
fun (n/2); {
result + = q * q + a[r];
fun(n/2);
}
return count; }
} }
(a) O(1) (b) O(n) return result;
(c) O(n2) (d) O(nlogn) }
(a) O(n) (b) O(n2)
[MCQ] (c) O(nlogn) (d) None of the above
2. What is the time complexity of an efficient algorithm
to determine if an integer array x contains two [MCQ]
5. Consider an stack of having ‘n’ elements, inorder to
consecutive integers? reverse the elements in stack first pop off the elements
(a) O(n) if x is sorted, (or) O(nlogn) otherwise one by one from stack and enqueue them into a queue.
(b) O(nlogn) whether x is sorted or not sorted Then deque the elements one by one from the queue
(c) O(n) if x is sorted (or) O(n2) otherwise and push them back onto the stack, then what is the
(d) O(n2) whether or not x is sorted. time complexity of above operation?
(a) O(n) (b) O(n2)
(c) O(nlogn) (d) O(logn)
2

Q.6 to 10 Carry TWO Mark Each


[MCQ] a = n;
6. A = {a1, a2 …… an} is an array consisting of ‘n’ distinct while (a > 0)
integers and ‘a’ is an integer, design an efficient {
algorithm to determine whether there are two elements b = n;
in ‘A’ whose sum is exactly ‘a’. Find the time while (b > 0)
complexity of the algorithm which was designed. {
(a) O(nlogn) (b) O(n2) Add = Add + 10;
(c) O(n) (d) none of these b = b/2
}
[MCQ] a = a – 10;
7. Consider the following function foo()____? }
void foo(int x) (a) O(n2) (b) O(n)
{ (c) O(nlog2n) (d) O(n2logn)
int a, b, c, d;
for (a = 0; a < 1000; a++)
{ [MCQ]
for (b = 0 b< n; b++) 9. Consider the following Snippet of code.
{ Addn = 0;
for (c = 0; c < b; c++) for (a = 1 to n)
printf(“%d”,c) for (b = a to 2n)
} Addn = Addn + 1;
}
What is the time complexity of above given code?
}
What is the worst case running time of the function (a) O(nlogn) (b) O(n)
foo() for any positive value of x? (c) O(n2) (d) O(n2logn)
(a) O(n) (b) O(n2)
3
(c) O(n ) (d) O(1) [MCQ]
10. Consider T(n + 1) = T(n) + 4n and T(n) = 1 then what
[MCQ] is the value of T(n) in terms of n?
8. What is the running time of the following c-program. (a) n2– n + 1 (b) 2n2– 2n + 1
Assume that n ≥ 1 and time complexity as a function (c) 2n – 2n + 1
2
(d) None of the above
of n.
3

Answer Key
1. (a) 6. (a)
2. (a) 7. (b)
3. (a, b, c, d) 8. (c)
4. (b) 9. (c)
5. (a) 10. (b)
4

Hints and Solutions

1. (a) 5. (a)
When n < = 0 which will return 0. Popping off all elements from stack and enqueuing
∴ O(1) them into queue will take O(n) time. Similarly
When n > = 2000, returns 1 enqueuing all n elements from queue and push them
∴ O(1) onto stack, again it will take O(n)
When O < n < 2000, Here loop will run for a constnat Total time = O(n + n)
= O(2n)
time. Hence time complexity for large value of n is
= O(n)
O(1) and the complexity of above code is O(1)
∴ option (a) is correct.
6. (a)
Firstyly sort the given array ‘A’ in O(nlogn) time, after
2. (a) sorting take two pointers and start them from the left
In order to check consecutive integer in a sorted array and right extreme of the sorted array ‘A’ and move
x, check them inwords. i.e… the left pointer moving right and
for (a = 0; a ≤ n; a++){ the right pointer morining left. Check the sum of two
if(x[a] = = x[a + 1] – 1) number pointed by these pointers. If the sum is equal
{ to x, then stop and return it otherwise continue above
process till the pointers meet. This process takes time
print(“exixts”);
complexithy of O(n)., So the total time taken is
Break O(nlogn).
} ∴ option (a) is correct.
}
This can be done in O(n) for sorted array. 7. (b)
for uinserted array, firstly sort the array in O(nlogn) 999 n–1  b–1 
time and then apply above algorithm. foo ( n )    1 = O(n 2 )
a =0 b =0  c =0 
∴ correct option is (a).

8. (c)
3. (a, b, c, d)
As we can see that, inner loop will be executing log2n
(a) yes, Inserting, Deleting in arrays takes O(N)
time. Containing N elements time for every a.
(b) yes, Inserting, deleting in string takes O(N) time, n n n
containing N-elements. b = n, , ...... ⇒ log2n series
2 4 n
(c) yes, Space complexity for each operation in a
linked list O(1) outerloop: a = n, n – 10, n – 20 ….. 1
(d) yes, The worst case time complexity of binary n
search tree is O(n). ∴ O   = O(n )
10 
4. (b) ∴ Time complexity (T.C) = O(log2n)
p runs for n times(x)
q runs for 7 times Hence option (c) is correct.
r runs for n times (x)
∴ 7n2 ⇒ O(n2)
5

9. (c) T(n) = T(n – 2) + 4 (n – 2) + 4 (n – 1)


n 2n n
 (1) =  (1 + 1 + 1 + 1 + ....( 2n – a + 1) times ) T(n) = T(n – 3) + 4 (n – 3) + 4 (n – 2) + 4(n – 1)
a =1b=1 a =1 = T(n – k) + 4(n – k) + 4 (n – k + 1) + ……
n n n n
= ( 2n – a + 1) = 2n(1) –  a +(1) 4 (n – 3) + 4 (n – 2) + 4 (n – 1)
a =1 a =1 a =1 a =1 let n – k = 1 ⇒ k = n – 1
n ( n + 1)
= 2n ( n ) – +n T(n) = T(1) + 4 (1) + 4(2) + 4(3) + ….. 4 (n – 2) + 4 (n
2
= O(n2) – 1)
∴ option (c) is correct. = 1 + 4 {1 + 2 + 3 + …… n – 1}
4n ( n –1)
10. (b) =1+ ⇒ 1 + 2n(n – 1)
2
Given
T(n + 1) = T(n) + 4(n) = 2n2 – 2n + 1

T(n) = T(n – 1) + 4 (n –1) ∴ option (b) is correct.

For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4

You might also like