0% found this document useful (0 votes)
7 views

final sem 24

Uploaded by

yocepi7889
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

final sem 24

Uploaded by

yocepi7889
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Birla Institute of Technology & Science, Pilani

Work Integrated Learning Programmes Division


Second Semester 2023-2024

Comprehensive Examination
(EC-3 Regular)

Course No. : SE ZG519


Course Title : Data Structures and Algorithms Design
Nature of Exam : Open Book
Weightage : 40% No. of Pages = 6
Duration : 2 ½ Hours No. of Questions = 7
Date of Exam : 18/05/2024 (FN)
Note to Students:
1. Please follow all the Instructions to Candidates given on the cover page of the answer book.
2. All parts of a question should be answered consecutively. Each answer should start from a fresh page.
3. Assumptions made if any, should be stated clearly at the beginning of your answer.

Q-1 Taarak Mehta’s boss tries to confuse genius Taarak. Boss asks Mr. Mehta to write an algorithm 5
whose time complexity is as follows. Marks
T(n) = T(n - 1) + T(n - 2) + O(n)

Write such an algorithm for Taarak.

Q-2 Prof. Virus has a single linked-list of students’ names. He asked Rancho to bring all odd- 5
positioned names together and even-positioned names together. Prof. Virus restricted Rancho Marks
from using any additional data structure. However, he is free to use additional pointer variables
to linked list nodes. He also instructed that the sequence of students at odd positions must be
same as input list. Similarly, the sequence of students at even positions must be same as input
list. Header to the linked list is given. Write the complete algorithm “Rancho” as your answer. In
the following sample inputs/outputs are given for your understanding. However, your algorithm
must work for all possible inputs.

Sample input1:
Raju → Rancho → Farhan → Pia → Chatur
Sample output1:
Raju → Farhan → Chatur → Rancho → Pia

Sample input2:
Raju → Rancho → Farhan → Pia → Chatur → Milli
Sample output2:
Raju → Farhan → Chatur → Rancho → Pia → Milli

Algorithm Rancho(header)
curr1 ← header
if curr1 = NULL then
Print “List empty”
Exit
end if
//Complete the algorithm

Q-3 Prof. Bhide gave a stack containing a string to Tapu and asked him to write an algorithm that 5
considers ‘*’ as backspace and gives the final output. Prof. Bhide restricted Tapu from using any Marks
additional data structure. However, he is allowed to use such variables that can store only one
value. He gave a hint that write the algorithm in a recursive manner. Write the complete Support
algorithm as your answer. In the following sample input/output is given for your understanding.
However, your algorithm must work for all possible inputs.
Sample input: BIT*SPILAN*IW (top of the stack pointing to W)
Sample output: BISPILAIW (top of the stack pointing to W)

Note: Assume that * will not be there at the start or end of the input string and * will not be there
in consecutive places in the input string.

Algorithm push(s,o):
top ← top+1
s[top] ← o
Algorithm pop(s):
if top = 0 then
Print “stack-empty”
Exit
end if
e ← s[top]
s[top] ← NULL
top ← top - 1
return e
Algorithm Tipendra(string x)
for i = 1 to x.length do
push(s,x[i])
Support()
Algorithm Support(s)//String characters are already pushed in stack ‘s’
//Complete the algorithm

Q-4 Prof. Forget has a map of the school. This map contains vertices as classrooms and edges as 5
energy required to go from one classroom to another classroom. Prof. gave a map to you and Marks
asked you to visit all the classes to announce that exam will be conducted tomorrow. You do not
want to spend too much energy on this task due to these summer days. Apply Kruskal’s
algorithm, show derivation as per the following table, and find the minimum energy required to
visit all the classrooms.
Steps Edges considered/rejected Connected components

0 – {1}, {2}, {3}, {4}, {5}, {6}, {7}

… … …

… … …

Total Energy Required = __?

Q-5 Prof. Forget fell asleep while writing the Merge sort algorithm. He made two mistakes in writing 5
the merge algorithm as underlined. He wants to see the effects of these mistakes on the output of Marks
the quick sort algorithm for the input array arr = {7, 16, 7, 14, 13, 20, 15, 2, 5}. Write the output
array only as your answer.

Note: Partial marking will not be done for this question.


MERGE_SORT(arr, beg, end)
if beg < end then
mid ← (beg + end)/2
MERGE_SORT(arr, beg, mid)
MERGE_SORT(arr, mid + 1, end)
MERGE (arr, beg, mid, end)
END MERGE_SORT

Merge(arr, beg, mid, end){


n1 ← mid - beg + 1
n2 ← end - mid
LeftArray[n1] //temporary array
RightArray[n2] //temporary array
for i = 0 to n1 do
LeftArray[i] ← arr[beg + i]
for j = 0 to n2 do
RightArray[j] ← arr[mid + 1 + j]
i ← 0 /* initial index of first sub-array */
j ← 0 /* initial index of second sub-array */
k ← beg /* initial index of merged sub-array */
while i < n1 and j < n2 do
if(LeftArray[i] <= RightArray[j])then
arr[k] ← LeftArray[i]
i ← i + 1
else
arr[k] ← LeftArray[i]//in place of arr[k] ← RightArray[j]
i ← i + 1 //in place of j ← j + 1
end if
k ← k + 1
end while
while i < n1 do
arr[k] ← LeftArray[i]
i ← i + 1
k ← k + 1
end while
while j < n2 do
arr[k] ← RightArray[j]
j ← j + 1
k ← k + 1
end while

Q-6 Apply Dijkstra’s algorithm on the following graph by considering vertex ‘6’ as the source. As 5
per the following table, write the set of labels for each vertex, where the label contains (previous Marks
vertex, distance).

(Note: Partial marking will not be done for this question.)


1 (6, 100)

5 (6, 250)

6 (-,0)

Q-7 During summer days, Harry and his friends are playing on the stairs of Hogwarts University. 10
They are able to climb 1/2/3 steps during each jump. Prof. Dumbledore saw them. In place of Marks
stopping them, Prof. gave them the following task for their betterment. He asked Harry and his
friends to derive an algorithm to find the minimum number of jumps required to climb the stairs
using dynamic programming. Write the algorithm solution as your answer.
E.g., If the stair has 9 steps, one can climb the stair with 3 jumps (i.e., 3 steps + 3 steps + 3 steps)

Algorithm solution (int n) // n is the total number of steps in stairs


// Write your algorithm here…

******

You might also like