0% found this document useful (0 votes)
17 views5 pages

Mid Sem 23

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)
17 views5 pages

Mid Sem 23

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/ 5

Birla Institute of Technology & Science, Pilani

Work Integrated Learning Programmes Division


First Semester 2023-2024

Mid-Semester Test
(EC-2 Regular)

Course No. : SEZG519


Course Title : Data Structures and Algorithms Design
Nature of Exam : Closed Book
Pattern of Exam : Typed Only
Weightage : 35% (70 Marks) No. of Pages =
Duration : 2 Hours No. of Questions = 5
Date of Exam : 23/09/2023 (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.

Please Note:
A maximum of 12 main questions with Q.Nos. 1 to N. Please do not include any objective
type questions. Please do not include any reference material like data tables in the question
paper, as all Mid-Semester Test are completely Closed Book examinations.

Q.1 Mr. Rahul does not like prefix expressions. However, Prof. Modi always gives him only 15 Marks
Set (A) prefix expressions and asks him to find the answer. You are appointed to help Mr. Rahul
in converting the received prefix expression to an infix expression using stack. Complete
the prefixtoinfix algorithm for the same using stack. You need to write prefixtoinfix
algorithm only as your answer.
Sample input: *+XY-ZW
Sample output: ((X+Y)*(Z-W))

Algorithm push(o):
top ← top+1
s[top] ← o
Algorithm pop( ):
if top = 0 then
indicate that a stack-empty error has occurred
e ← S[top]
s[top] ← NULL
top ← top - 1
return e
Algorithm concat(input ,input …, input )
1 2, n

return (input || input ||...|| input )


1 2, n

Algorithm operator(input)
if input = ‘+’ or input = ‘-’ or input = ‘*’ or input = ‘/’ or input = ‘^’ then
return TRUE
else
return FALSE
Algorithm prefixtoinfix(prefixexp):
//Complete the algorithm
Q.1 Prof. Forget is supposed to teach an algorithm to convert postfix expressions to infix 15 Marks
Set (B) expressions. However, he forgot the algorithm during the lecture, as always. You are
appointed to help Prof. Forget in recalling the algorithm for converting postfix to infix
expressions. Complete the postfixtoinfix algorithm using stack. You need to write
postfixtoinfix algorithm only as your answer.
Sample input: XY+ZW-*
Sample output: ((X+Y)*(Z-W))

Algorithm push(o):
top ← top+1
s[top] ← o
Algorithm pop( ):
if top = 0 then
indicate that a stack-empty error has occurred
e ← S[top]
s[top] ← NULL
top ← top - 1
return e
Algorithm concat(input ,input …, input )
1 2, n

return (input || input ||...|| input )


1 2, n

Algorithm operator(input)
if input = ‘+’ or input = ‘-’ or input = ‘*’ or input = ‘/’ or input = ‘^’ then
return TRUE
else
return FALSE
Algorithm postfixtoinfix(postfixexp):
//Complete the algorithm

Q.2 Prof. Forget asks the last bench student to come to the first bench at the beginning of his 15 Marks
Set (A) class. He has a singly linked list of students’ names as per the benches’ order. Complete
the bringlastbencher algorithm to bring the last bencher to the first bench. You need to
fill the blanks only as your answer.
Sample input: Salman → Shahrukh → Akshay → Ranbir → Ranveer → Emraan
Sample output: Emraan→Salman → Shahrukh → Akshay → Ranbir → Ranveer

Algorithm bringlastbencher(header)
if header=NULL or header.next=NULL then
Exit
ptr1← header
ptr2← NULL
while ptr1.next ≠ NULL do
ptr2 ← ptr1
ptr1 ← ______________ //(4 marks)
endwhile
_______________ //(4 marks)
_______________ //(4 marks)
head ← ________ //(3 marks)

Q.2 Prof. Bhide stores the names of his students in a singly linked list. He asks students to 15 Marks
Set (B) perform class assignments in groups of two-two students. He interchanges the seats of
group members after each class. Prof. Bhide asked Mr. Jethalal to write an algorithm to
perform this interchange. Mr. Jethalal has written the following algorithm.
Is it a correct algorithm? Answer in yes or no. (3 Marks)
If no, then write the numbers of incorrect lines and also mention the corrected version of
these lines. (12 Marks)
Sample input: Tapu→Sonu→Pinku→Goli→Gogi→Raju
Sample output: Sonu→Tapu→Goli→Pinku→Raju→Gogi

Algorithm Arrange(header)
1 if header=NULL or header.next=NULL then
2 Exit
3 ptr1← header
4 ptr2← ptr1.next
5 while ptr2 ≠ NULL do
6 temp ← ptr1.data
7 ptr1.data ← ptr2.data
8 ptr2.data ← temp
9 ptr1 ← ptr1.next
10 ptr2 ← ptr2.next

Q.3 A movie of Mr. Rajnikant is released and going housefull all the days. A multiplex has 15 Marks
Set (A) decided to give one ticket at a time to each person in a queue. Each person takes a ticket
in exactly 1 second. S/he rejoins the queue from the back if s/he needs more tickets. The
person leaves the queue if s/he does not want to buy any more tickets. You are given an
integer array "t" of length n where t[i] is the number of tickets ith person wants to buy.
Complete the time algorithm to find the time taken by ith person to finish buying the
tickets. You need to write time algorithm only as your answer.
Sample input: [2,3,2], i = 3
Sample output: 6
Explanation:
Second 1-[3,2,1], Second 2-[2,1,2], Second 3-[1,2,1], Second 4-[2,1] and 1st person
leaves the queue, Second 5-[1,1], Second 6-[1] and 3rd person leaves the queue.

Algorithm enqueue(q, o):


if rear = N then //we assume that N is infinite
indicate that a queue-full error has occurred
return
rear ← rear+1
q[rear] ← o
if front = 0 then
front = 1
Algorithm dequeue(q):
if front = 0 then
indicate that a queue-empty error has occurred
return NULL
e ← q[front]
q[front] ← NULL
if front = rear then
front ← 0
rear ← 0

else
front ← front +1
return e
Algorithm time(t, i):
//Complete the algorithm

Q.3 Mr. Sandeep is fond of playing cards. He has an array of integer values of cards he has. 15 Marks
Set (B) He has created a card game that follows these steps.
Step 1) Take the last card from the deck, reveal it, and take it out of the deck.
Step 2) If there are still more cards in the deck, then put the next card at the top of the
deck.
Step 3) If there are still more cards in the deck, then go back to step 1. Otherwise, stop.

Complete the revealorder algorithm to find out the order of revealing cards. You need to
write revealorder algorithm only as your answer.
Sample Input1: [17, 13, 11, 2, 3, 5, 7]
Sample Output1: 17 11 3 7 2 13 5
Explanation: Reveal 17, Add 13 at the top, Reveal 11, Add 2 at the top, Reveal 3, Add 5
at the top, Reveal 7, Add 13 at the top, Reveal 2, Add 5 at the top, Reveal 13, Add 5 at
the top, Reveal 5.
Sample Input2: [17, 13, 11, 2, 3, 5]
Sample Output2: 17 11 3 13 5 2
Explanation: Reveal 17, Add 13 at the top, Reveal 11, Add 2 at the top, Reveal 3, Add 5
at the top, Reveal 13, Add 2 at the top, Reveal 5, Add 2 at the top, Reveal 2

Algorithm enqueue(integer o):


if rear = N then //we assume that N is infinite
indicate that a queue-full error has occurred
return
rear ← rear+1
q[rear] ← o
if front = 0 then
front = 1
Algorithm dequeue( ):
if front = 0 then
indicate that a queue-empty error has occurred
return NULL
e ← q[front]
q[front] ← NULL
if front = rear then
front ← 0
rear ← 0
else
front ← front +1
return e
Algorithm revealorder(integer cards[ ]):
//Complete the algorithm

Q.4 Prof. X gave you preorder and inorder traversals of your tree. Write postorder traversal 15 Marks
Set (A) for this tree. (Note: Partial marking will not be done for this question.)

Preorder: 94, 33, 25, 19, 28, 35, 50, 54, 60, 55, 70
Inorder: 19, 25, 28, 33, 35, 94, 54, 50, 55, 60, 70
Q.4 Prof. Forget gave you postorder and inorder traversals of your tree. Write preorder 15 Marks
Set (B) traversal of the tree. (Note: Partial marking will not be done for this question.)
Inorder: 19, 25, 82, 33, 35, 94, 54, 50, 56, 60, 71
Postorder: 19, 82, 25, 35, 33, 54, 56, 71, 60, 50, 94
Q.5 Determine the value of x and y (in terms of n) at the end of the execution of the 10 Marks
Set (A) following algorithms algo1 and algo2. (5 marks + 5 marks)
(Note: Partial marking will not be done for this question.)

Algorithm algo1(integer n) Algorithm algo2(integer n)


x←0 y←0
for i = 0 to n-1 do for i = 0 to n-1 do
for j = 0 to n-1 do for j = 0 to i-1 do
x ← x+1 y ← y+1

Q.5 Find out the value of x and y (in terms of n) at the end of the execution of the following 10 Marks
Set (B) algorithms algo1 and algo2. (5 marks + 5 marks)
(Note: Partial marking will not be done for this question.)

Algorithm algo1(integer n) Algorithm algo2(integer n)


x←0 y←0
for i = n/2 to 1 do for i = n/2 to 1 do
for j = n/2 to 1 do for j = n/2 to i do
x ← x+1 y ← y+1

You might also like