Algorithms Test Paper -1
Algorithms Test Paper -1
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
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
For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if