Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Birla Institute of Technology & Science, Pilani

Work Integrated Learning Programme Division


M.Tech (Data Science & Engineering)
First Semester 2023-2024
Mid-Semester Examination (EC2 Makeup) - KEY

Course Number : DSECLZG519


Course Title : Data Structures and Algorithms Design
Type of Exam : Closed Book No. of Pages: 4
Weightage : 30 Marks No. of Questions: 6
Duration : 2 Hours
Date of Exam : 3rd Feb 2024 Session: AN

Note:
1. Start each answer from a fresh page. All parts of a question should be answered
consecutively. Number the questions appropriately while uploading the answers.
2. Draw necessary diagrams wherever required.
3. Cite your assumptions clearly. Sheets uploaded without name & registration number will not
be evaluated.

Question 1 [1.5M+1.5M+0.5M+1.5M=5M]
𝑛
A) Solve the recurrence relation: 𝑇(𝑛) = 8𝑇 ( ) + √𝑛 𝑙𝑜𝑔−2 𝑛
2

Use Master’s theorem:


a=8, b=2, k=0.5, p = - 2
log 𝑏 𝑎 = log 2 8 = 3 and k=0.5 which means log 𝑏 𝑎 > 𝑘, hence we are in Case (1) of Master’s
theorem. → 1M
ϴ(𝑛log𝑏 𝑎 ) = ϴ(𝑛3 ) → 0.5M
B) Evaluate the validity/correctness of the following “Delete an element from a doubly linked
list (L) from a specific position (k)” and explain your reasoning. If its incorrect, provide the
necessary modifications to make them valid.

Incorrect → 0.5M, just mentioning incorrect without pointing the error will get 0
next[k]  next[q]
next[q]  null
Corrected/Modified Version → 1M
next[q]  next[k]
prev[next[k]]  q
OR
next[prev[k]]  next[k]
prev[next[k]]  prev[k]

C) Mention what type of Data structure can be used to implement back button on the browser.
Stack 0.5M , wrong 0

D) Evaluate ABC+*CBA-+* using Stack where A=1, B=2 and C=3. Show all the steps clearly.

123+*321-+* , Steps → 1M
Answer 20 → 0.5M

***********************
Question 2 [2M+3M=5M]
A) We have a circular queue (using array), Q of size 5. What will be the final contents of the
queue and the positions of front and rear pointers after executing the below code,

for (int i = 1; i <= 5; i++)


Q.enqueue(i);
for (int j = 1; j <= 3; j++)
{
Q.enqueue(Q.dequeue());
Q.dequeue();
}

Both the contents and their positions in the array should be perfect – 1 Mark (no partial marks)
front and rear pointers to be shown correctly – 1 Mark

A) Show how to implement one queue using two stacks. Analyze the running time of the queue
operations.
enQueue(q, x):
• While stack1 is not empty, push everything from stack1 to stack2.
• Push x to stack1 (assuming size of stacks is unlimited).
• Push everything back to stack1.
Here time complexity will be O(n)
deQueue(q):
• If stack1 is empty then error
• Pop an item from stack1 and return it
Here time complexity will be O(1)

Valid algo/pseudocode → 2M, Complexity → 1M

***********************

Question 3 [1.5M+1.5M+2M=5M]

A) Suppose we have a binary max-heap that has 2000 elements (nodes). Assume that the max-
heap satisfies all the heap properties.

i) What will be the height h of this max-heap?

h = floor (log2 n) = floor (log2 2000) = 10 → 0.5M, Any other answer → 0M

ii) How many more nodes can we add to the max-heap without changing the height h of
the max-heap? (Assume that the max-heap satisfies all the max-heap properties after every
node insertion)

• Maximum number of nodes in max-heap of height h = 2h+1 – 1


• Maximum number of nodes in max-heap of height 10 = 211 - 1 = 2047
• We can add 47 more nodes to the max-heap without changing its height.
Correct answer as above with explanation 1M, Any other vague answer 0M

B) Build-Max-Heap procedure takes an unordered array A[1..n] and converts it into a binary
max-heap in Θ(n) time. Suppose the input to the Build-Max-Heap procedure is array A =
[2, 1, 0, 9, 7, 5, 8, 6, 4, 3]. What will be the elements in array A after
Build-Max-Heap procedure terminates?

A = [9,7, 8, 6, 3, 5, 0, 1, 4, 2]. In order to get this most students would have drawn the heap
and then represented it as an array. If not also it is fine to award 2M.

Above answer → 2M, Any other answer 0M

C) A min-heap array is a [0...n−1]-indexed array with all n elements filled with the property that
ai ≤ min(a2i, a2i+1). Write a recursive procedure to fill a minheap array from a Binary Search
Tree (BST) efficiently.
It is just a slight modification of InOrder Traversal.

Any other complete answer → 2M, otherwise 0M

***********************
Question 4 [3M+2M=5M]

A) Given the below tree traversals:


• Inorder traversal = {4, 2, 1, 7, 5, 8, 3, 6}
• Post-order traversal = {4, 2, 7, 8, 5, 6, 3, 1}

Construct a Binary Tree and Find a Preorder traversal.

Steps and final correct tree → 2M, no partial marking (even if one node is wrong, 0)

Preorder Traversal is: {1, 2, 4, 3, 5, 7, 8, 6} → 1M

B) What is the minimum size of the array required to store the below given Binary Tree. Show
the array.
Minimum array size is 12. Contents of the array is

Size 1M, Contents 1M

***********************
Question 5 [2.5M+2.5M=5M]
A) The adjacency matrix representation of a graph G is given below. Draw the corresponding
graph and perform BFS traversal by considering ‘1’ as the source. Show all your steps.

→ 1M
BFS steps with queue and getting 1,2,4,6 and further we cannot proceed as we started from
source. If we use source as 3, then we can complete the traversal with 3,1,2,5,4,6
Starting with 1 as source and performing traversal and concluding that we cannot reach all →1
M, starting with another node like 3 and showing the traversal order → 0.5M

B) Suppose there are 12 courses: C12, C16, C23, C31, C33, C34, C35, C42, C43, C45, C52,
C51, which you have to take to finish your prestigious degree. C12 and C16 are pre-
requisites for C23, C23 is prerequisite for C31, C33, C34, C35 and C33 is prerequisite for
C42 and C43, C42 is prerequisite for C43, C45 is prerequisite for C52, C42 is prerequisite
for C51. Model this using an appropriate data structure and explain what approach would
be suitable to find the order in which these courses have to be taken.
Graph → 1M, incorrect graph → 0M
Valid Algorithm/pseudocode/explanation (with or without mentioning topological
ordering) → 1.5M, incorrect solution → 0M
***********************

Question 6 [3M+2M=5M]
A) Consider a hash table consisting of N =11 slots, and suppose non-negative integer key
values are hashed into the table using the hash function h1():
int h1(int key) {

int x = (key +7) * (key +7);


x=x/16;
x = x+key.
x =x%11;
return x;
}

Suppose that collisions are resolved by using linear probing. Show the insertion of the integer key
values 43,23,1,0,15,31,4,7,11,3, in the order given.

M=11
Hashed Values by substituting the key in the function: (1M)
H1(43) = 1 H1(23) = 2 H1(1) = 5 H1(0) = 3 H1(15)=1
H1(31) = 0 H1(4)=0 H1(7)= 8 H1(11)= 9 H1(3) = 9

Linear Probing: (2M)

31 43 23 0 15 1 4 7 11 3
0 1 2 3 4 5 6 7 8 9 10

B) Consider a Single linked list. Assume that we have a pointer to the last element in this
Single linked list. What is the worst-case time complexity of deleting the last element in
this list? Provide reason for your answer.

The worst-case time complexity of deleting the last element in a singly linked list when we have a pointer to the
last element is O(n), where n is the number of elements in the list. 1M

Reason: (1M) To delete the last element, we need to traverse the entire list to reach the second-to-last node.

• Since we only have a pointer to the last element, we cannot directly access the second-to-last node without
traversing the list from the beginning.
• Traversing the list from the head to the second-to-last node takes O(n) time complexity, where n is the
number of elements in the list.
• Once we reach the second-to-last node, we can update its next pointer to null, effectively removing the
last node from the list.

****************************** All the Best **********************************

You might also like