You are on page 1of 3

Roll No.

: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Amrita Vishwa Vidyapeetham
Amrita School of Engineering, Coimbatore
B.Tech First Assessment Examinations – April 2022
Fourth Semester
Computer Science and Engineering
19CSE 212: Data Structures and Algorithms

Duration: Two hours Maximum: 50 Marks

answer Key

1. if you start with an array with one element and add a second element, you have to
copy the first element into another array. If you add a third element, you have to copy
the other two elements. If you add a fourth element, you have to copy the first three.
This adds up to 1+2+3...+N, which is equal to N(N+1)/2, which is in O(N2)
If you resize by doubling the array, then when you get some power of two size N, N/2
will have been copied 0 times, N/4 will have been copied once, N/8 will have been
copied twice, and so on. The sum of 0N/2 + 1N/4 + 2N/8 + 3N/16... is in O(N)
2. You could create an additional array B of size n. Initially set all elements of the array
to 0. Then loop through the input array A and increase B[A[i]] by 1 for each i. After
that you simply check the array B: loop over A and if B[A[i]]>1 then A[i] is repeated.
You solve it in O(n) time at the cost of memory which is O(n) and because your
integers are between 1 and n−5.
3. x = S.pop()
if (x < S.top()){
x = S.pop()
if (x < S.top())
x = S.pop()
}else{
S.pop()
if (x < S.top())
x = S.pop()
}

x is the largest interger


let x equal to stack first elment
if x is less than top of stack
then x is equal to pop of stack
if x is less than top of stack
then x is equal to pop of stack
else x is greate than top of stack
drop (pop) the stack
if x is less than top of stack
then x is equal to pop of stack
once I know that the greatest is returned from B and A there is one possibility that the
greatest integer is not between those two integers (B and A) that is C so.. I have two
possibilities of getting the greaters number between three -> 2/3
4. Finds the median element of an array of n integers, O(n^2). Finds the middle element of
the array and exits the algorithm without sorting. With the time complexity of O(n^2)
5. itemHopper.itemName != NULL, itemHopper.itemName == X, itemHopper.stock, “No
such item”,
Case 1:O(n), Case 2: O(n).
6. f(n) = O(g(n)), g(n) = O(f(n)), f(n) = O(g(n))
7. a. O(1)
Solution: To get O(1), for loop has to be returned in the first iteration itself. To break out of a
for loop, you can use the end loop, continue, resume, or return statement.
b. O(n log n)

Solution: To get O(n log n), first loop for(i=0; i<n; i++) For each i, we can have one more
loop running for (j=0; j<n ; j=j*2)

c. O(log n)

Solution: Insert a condition such that the for loop terminates when i reaches log n value.

d. O(n^2)

Solution: To get O(n^2), an additional loop with j<=n can be introduced.

e. O(2^n)

Solution: To get O(2^n), first loop iterates for i=0 to i=n. For each i, we can print 0 to 2^i.
This leads to complexity of O(2^n). More explanation

8. For sorting a linked list:

i=0

while i != n:

Traverse the list to reach the ith element (for or while loop)

Remove ith element using removeafter function

for j = i; j>0; j--:

if ith element <= jth element insertAfter j-1th element


else continue with comparison

i++

3 marks

Reason for effectiveness: 2 marks

1. There is no need for shifting or swapping like in an array, the

9. Two lists can be merged in O(n) time O(n) space is required. The idea is to pair
up k lists and merge each pair in linear time. After the first cycle, K/2 lists are left
each of size 2×N. After the second cycle, K/4 lists are left each of size 4×N and so
on. Repeat the procedure until we have only one list left. - 3 marks

The time complexity of the above solution is O(n.log(k)) as the outer while loop in
function runs O(log(k)) times, and every time we are processing n elements. 2-
marks

10. Option 1, have topred at 0, topblue at n-1, and correspondingly define the push,
pop etc for the two stacks which grow towards the center. Other option is to start
from center with one stack growing left, and other growing right.

For this idea with some diagram – 2 marks

Clear adt with definition of push, pop, top, size, isempty – 3 marks

You might also like