Professional Documents
Culture Documents
Ec2m Dseclzg519 Key
Ec2m Dseclzg519 Key
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
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,
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)
***********************
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.
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)
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.
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.
***********************
Question 4 [3M+2M=5M]
Steps and final correct tree → 2M, no partial marking (even if one node is wrong, 0)
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
***********************
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) {
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
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.