UCS301-MST - Solution Dsa
UCS301-MST - Solution Dsa
Note: Attempt all Questions in sequence. Answer all sub-parts of each question at one place. Do mention
Page No. of your attempt at front page of your answer sheet. Assume missing data (if any).
Q.1 a) Determine the worst case time complexity of the following functions by finding the frequency (3)
i.e. the number of times each statement executes:
i) fun1(n) ii) fun2 (n)
{ {
for(int i=1; i<=n; i++) int i, j, k, s=0;
for(int j=1; j<=i; j++) for (i = 1; i <= n; i = i + 1)
for(int k=1; k<=n; k++) for (j = 1; j <= n; j = 2*j)
break; for (k = 1; k <= 2𝑛 ; k = 2*k)
} s = s + 1;
(1.5 mark) } (1.5 mark)
b) Write the recursive function of the binary search algorithm. Show the recursive calls made for (3+2)
searching the key element “8” in the given array A = {2, 5, 6, 7, 8, 9, 10, 15, 16, 18, 19, 20}.
Page 1 of 9
Q.2 (3+5)
Let Q be a variable of the structure named “queue”, void Fun(struct queue Q, char x)
which is used to maintain a circular queue. Q contains {
three data members, namely, a character array arr[] if (isEmpty(Q) ||
with MAXSIZE = 6 (Indexing starts from 0) and two Q.arr[Q.front]>x)
integers, viz., front and rear. Front points to the first {
element and rear points to the last element of the Enqueue(Q, x)
queue. The first element is inserted at MAX-1 position return;
in an initially empty queue and then queue grows }
towards left. char y = Dequeue(Q)
Fun(Q, x);
Page 2 of 9
if(y != Q.arr[Q.front])
a) Write the function Enqueue() which takes Q and an Enqueue(Q, y);
element to be inserted in Q as its input and Dequeue() }
which takes Q as its input and returns an element to be int main()
deleted from Q if any. {
while(!isEmpty(Q))
b) Let Q.arr[] = {‘D’, ‘A’, ‘M’, ‘A’, ‘G’, ‘E’}, where Q.front=5 Fun(Q, Dequeue(Q));
and Q.rear=0. isEmpty(Q) function returns true if Q is return 0;
empty. Execute the given code snippet (Fig 1) and give }
the contents of queue along with the values of front Fig 1
and rear after each of the first three iterations of the
while loop within main().
b)
After first iteration
Q.arr[] = {‘D’, ‘A’, ‘M’, ‘A’, ‘G’, ‘E’}, with Q.front=4 and Q.rear=5 (1 mark: 0.5 for front and 0.5 for Rear)
If a student has not written values of front and rear but pointing to the right element, then also it will
be considered.
Q3. (4)
Convert the infix expression A*(B&D/E)-F*(G%H/K) to Table 1
postfix expression using stack as an intermediate data Operator Precedence
structure. Show contents of the stack at each intermediate % Highest
step. Use the precedence table as shown in Table 1. *, /
+, -
& Lowest
Page 3 of 9
Q.4 a) Given an array A[]= {8, 7, 6, 1, 0, 9, 2} you have to sort the array in ascending order using Quick (4+2)
Sort algorithm by considering last element as Pivot. Show the intermediate steps. Determine
which kind of input will lead to worst case and what will be the worst-case complexity?
Page 4 of 9
(4)
Worst Case Complexity [Big-O]: O(n2), when input is sorted either ascending or descending.
It occurs when the pivot element picked is either the greatest or the smallest element. This
Page 5 of 9
condition leads to the case in which the pivot element lies in an extreme end of the sorted array.
One sub-array is always empty, and another sub-array contains n-1 elements. Thus, quicksort is
called only on this sub-array.
b) Given an array A consisting of 6s, 7s, and 8s such as A[]= {6,7,7,6,7,8,7,8,7,8,6,7,8,6,6} you have
to sort the array of 6’s, 7’s and 8’s in ascending order. You need to modify the partition procedure
of quicksort to place the elements at their correct position in the sorted order by iterating through
the array only once. Give suitable pseudocode/code for the same.
Let's divide the array into four sections using 3 pointer approach and name it low, medium and
high.
A [0……low-1] to fill 6s
A [low……mid-1] to fill 7s
A [mid…. high] unknown
A [high+1……n-1] only 8s
Initialize low=0, mid=0 and high = n-1 Scan the array using mid pointer:
If (A[mid]= =6): We swap value at A[mid] with value at A[low] and increase both low and mid
pointers by 1. (Shrinking the unknown range)
If (A[mid]= =7): Increment mid pointers by 1. (Further shrinking the unknown range)
If (A[mid]= =8): We swap value at A[mid] with value at A[high] and decrease high pointer by 1.
Now again traverse the array from the same position of mid pointer as the element at this index
has not been considered yet.
high = high – 1;
}
}
}
Second method
● In this approach we will be using two pointers(l and r) along with an iterator(i). The entire
array will be divided into three ranges:
● Index 0 to l-1 (range containing 6)
Page 6 of 9
● Index l to r (range containing unknown elements)
● Index r+1 to n (range containing 8)
● Initialise l=0 and r=n-1.
● Inside a for loop, make sure i<=r and do the following steps:
● If the i-th element is 6, swap it with arr[l] and increment l and i.
● If the i-th element is 8, swap it with arr[r] and decrement r (not i). The loop will
automatically check for the next updated value of arr[i].
● If the i-th element is arr[i], simply increment i and continue.
Q.5 a) Consider space efficient array representation (in row-major order) of sparse matrix with a (3)
header row. You need to multiply given sparse matrices: A[][] = {{5, 5, 5}, {0, 1, 5}, {0, 2, 18}, {2, 1,
14}, {3, 1, 15}, {3, 3, 4}} and B[][] = {{5, 5, 5}, {0, 1, 5}, {1, 2, 20}, {2, 0, 8}, {3, 1, 15}, {3, 3, 24}} and
show the output sparse matrix using space efficient array representation.
(2)
5 5 6
0 0 144
0 2 100
2 2 280
3 1 60
3 2 300
3 3 96
b) You are given two arrays A[] = {5, 6, 7, 8, 9} and B[] = {7, 8, 9, 6, 5}. Array A[] represents the
input queue and the array B[] represents the output queue. You need to determine if an array B[]
is the valid stack permutation of an array A[] or not. Justify your answer by showing the proper
sequence in which the operations will be called.
push(5), push(6), push(7), pop(), push(8), pop(), push(9), pop(), pop(), pop()
0.5 marks out of 2 for writing yes without any correct explanation
No marks are given if written B[] is not valid stack permutation of A[].
Page 7 of 9
Q.6 Consider a group of children playing hide-and-seek choose IT (the “chor”). They stand in a circle,
and the child (called the counter) recites some kind of a rhyme consisting of k words, starting
with himself/herself and moving on to the next child after reciting each word in a circular fashion.
The child at whom the rhyme ends is eliminated from subsequent rounds of counting. The
circular linked list should shrink as children are eliminated from counting. Counting for the next
round starts from the child following the eliminated child (in circular order). The process
continues until one child remains, who becomes IT. The children, numbered 1 . . . N, should be
represented by nodes in a circular linked list. The node of circular linked list has data field as data
and next pointer as next.
(7)
a) Your first task is to complete the following code to simulate the above activity. You need
not to write entire code just mention the statements corresponding to the fill in the blanks
(1-12) only. Each blank should consist of single statement only.
node * make_circle(node *head, int N) void find_IT(node *head, int counter, int k)
{ {
for (int i = 1; i ≤ N; i++) node *p= head, *q;
{ for(int i = 1; ___6_____; i++)
node * nn = new node(); p=p->next;
nn->data = i; while (____7_____)
nn->next = NULL; {
if (head == NULL) for (int j = 0; ___8____ ; j++)
_______1_______; {
else ______9________;
{ ______10_______;
node* r = head; }
while (_______2______) _____11________;
_______3_______; delete(___12_____);
_______4________; ____ 13________;
} }
_______5________;
} cout<<”Child who becomes IT is
return head; “<<______14____;
} }
(3)
a) 0.5 mark for each correct fill in the blanks
1. head = nn
2. r->next != head
3. r=r->next
4. r->next=nn
5. nn->next=head
7. p->next!=p
9. q=p
Page 8 of 9
10. p=p->next
12. p
13. p = q->next;
14. p->data or p
b) Your second task is to apply the above code to a group of 5 children with k = 7 and counter
= 3. You need to find the order in which the children are eliminated and then the child
who becomes IT.
Page 9 of 9