0% found this document useful (0 votes)
15 views23 pages

EndSem Key

The document contains multiple questions related to data structures and algorithms. It tests knowledge of expressions, linked lists, stacks, queues, trees, sorting, and hashing. The questions have increasing difficulty and provide additional marks for detailed solutions.

Uploaded by

foeforme
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)
15 views23 pages

EndSem Key

The document contains multiple questions related to data structures and algorithms. It tests knowledge of expressions, linked lists, stacks, queues, trees, sorting, and hashing. The questions have increasing difficulty and provide additional marks for detailed solutions.

Uploaded by

foeforme
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/ 23

Name: Roll No:

CS201- Data Structures and Algorithms EndSem Duration: 2 hr 10 minutes

Q1) What is the output of the following expression? [0.5 Mark]

5 7 * 3 + 9 3 / 6 7 - * +

[Note#1: A B + considered as A+B]


[Note#2: Push the values into the stack from left to right and perform the operations]
[Note#3: Detailed solution gets an additional 0.5 Mark for this question]

Ans: 35

Q1) What is the output of the following expression? [0.5 Mark]

5 7 + 3 * 9 3 / 6 4 + * -

[Note#1: A B + considered as A+B]


[Note#2: Push the values into the stack from left to right and perform the operations]
[Note#3: Detailed solution gets an additional 0.5 Mark for this question]
Ans: 6

Q1) What is the output of the following expression? [0.5 Mark]

7 7 + 3 * 9 3 / 4 5 + * -

[Note#1: A B + considered as A+B]


[Note#2: Push the values into the stack from left to right and perform the operations]
[Note#3: Detailed solution gets an additional 0.5 Mark for this question]

Ans: 15

Q1) What is the output of the following expression? [0.5 Mark]

7 7 * 3 + 9 3 / 4 5 - * +
[Note#1: A B + considered as A+B]
[Note#2: Push the values into the stack from left to right and perform the operations]
[Note#3: Detailed solution gets an additional 0.5 Mark for this question]

Ans: 49

Q2) struct node{

int data;
struct node *next;
};

[Note#1: Please consider that the following elements are already inserted in the Linked List.]

[1 Mark]

head → 45 → 32 → 65 → 74 → 99 |NULL| (NULL represents the end of the list)


struct node *p,*q,*t;
q=head;
p=q->next;
p=p->next->next;
q->next=p->next;
t=q->next;
printf ("%d", p->data);
printf ("%d", (q->data + t->data));
What are the print statement values ?
[Note#2: Enter the first print value in the first text box and enter the second print value in the second text
box, assume the direction of the text boxes from left to right]
[Note#3: Detailed solution gets an additional 1 Mark for this question]

Ans: 74, 144

Q2) struct node{

int data;
struct node *next;
}

[Note#1: Please consider that the following elements are already inserted in the Linked List.]

[1 Mark]

head → 45 → 32 → 65 → 74→ 99 |NULL| (NULL represents the end of the list)


struct node *p,*q,*t;
q=head;
p=q->next;
p=p->next->next;
q->next=p->next;
t=q->next;
printf ("%d", (p->data + q->data));
printf ("%d", t->data);
What are the print statement values ?
[Note#2: Enter the first print value in the first text box and enter the second print value in the second text
box, assume the direction of the text boxes from left to right]
[Note#3: Detailed solution gets an additional 1 Mark for this question]

Ans: 119, 99

Q2) struct node{

int data;
struct node *next;
}

[Note#1: Please consider that the following elements are already inserted in the Linked List.]

[1 Mark]

head → 45 → 32 → 65 → 74 → 99 → 15 |NULL| (NULL represents the end of the list)


struct node *p,*q,*t;
q=head;
p=q->next;
p=p->next->next;
q->next=p->next;
t=q->next;
printf ("%d", (p->data + q->data));
printf ("%d", t->next->data);
What are the print statement values?
[Note#2: Enter the first print value in the first text box and enter the second print value in the second text
box, assume the direction of the text boxes from left to right]
[Note#3: Detailed solution gets an additional 1 Mark for this question]

Ans: 119, 15

Q3) After performing the following operations on Queue and Stack data structure [queue size is set to 5
and stack size is set to 3].

[1 Mark]

(1) Enqueue () to the queue even numbers between 11 to 25.


(2) On getting the Queue exception by inserting the value, Dequeue() 2 values (i.e., Dequeue(),
Dequeue ())
(3) Push() each value to stack that are Dequeued in Step (2)
(4) Pop() all pushed values from the stack
(5) Enqueue() each value in the same order that is popped from Step (4)

Write down the elements in the Queue from Left to Right in the box

[Note#1: Please consider that the queue operations is following the real queue implementation, which
means that tail pointer is reducing after dequeuing an element]
[Note#3: Detailed solution gets an additional 1 Mark for this question]

Ans: 16, 18, 20, 14, 12

Q3) After performing the following operations on Queue and Stack data structure [queue size is set to 7
and stack size is set to 3].
[1 Mark]
(1) Enqueue () to the queue even numbers between 11 to 25.
(2) On getting the Queue exception by inserting the value, Dequeue() 2 values (i.e., Dequeue(),
Dequeue ())
(3) Push() each value to stack that are Dequeued in Step (2)
(4) Pop() all pushed values from the stack
(5) Enqueue() each value in the same order that is popped from Step (4)

Write down the elements in the Queue from Left to Right in the box.

[Note#1: Please consider that the queue operations is following the real queue implementation, which
means that tail pointer is reducing after dequeuing an element]
[Note#3: Detailed solution gets an additional 1 Mark for this question]

Ans: [12, 14, 16, 18, 20, 22, 24]

Q3) After performing the following operations on Queue and Stack data structure [queue size is set to 7
and stack size is set to 3].
[1 Mark]

(1) Enqueue () to the queue odd numbers between 12 to 26.


(2) On getting the Queue exception by inserting the value, Dequeue() 2 values (i.e., Dequeue(),
Dequeue ())
(3) Push() each value to stack that are Dequeued in Step (2)
(4) Pop() all pushed values from the stack
(5) Enqueue() each value in the same order that is popped from Step (4)
Write down the elements in the Queue from Left to Right in the box?

[Note#1: Please consider that the queue operations is following the real queue implementation, which
means that tail pointer is reducing after dequeuing an element]
[Note#3: Detailed solution gets an additional 1 Mark for this question]

Ans: [13, 15, 17, 19, 21, 23, 25]


Q4) The Postorder of binary search tree as follows:

16, 12, 40, 42, 75, 88, 86, 54, 22 [1 Mark]

Write down the Inorder and Preorder traversal for the above?

[Note#1: Detailed solution gets an additional 1 Mark for this question]

ANS:

Inorder: 12, 16, 22, 40, 42, 54, 75, 86, 88

Preorder: 22, 12, 16, 54, 42, 40, 86, 75, 88

Q4) The Postorder of binary search tree as follows:

29, 39, 66, 52, 69, 84,81, 67, 50 [1 Mark]

Write down the Inorder and Preorder traversal for the above?

[Note#1: Detailed solution gets an additional 1 Mark for this question]

ANS:

Inorder: 29, 39, 50, 52, 66, 67, 69, 81, 84

Preorder: 50, 39, 29, 67, 52, 66, 81, 69, 84

Q4) The post order of binary search tree is given by

37, 47, 50, 48, 42, 98, 91, 72, 70 [1 Mark]

Write down the Inorder and Preorder traversal for the above?

[Note#1: Detailed solution gets an additional 1 Mark for this question]

ANS:

Inorder: 37, 42, 47, 48, 50, 70, 72, 91, 98

Preorder: 70, 42, 37, 48, 47, 50, 72, 91, 98


Q5) What will be the output at the end of the fifth iteration of selection sort while sorting the following
numbers in ascending order?

9, 5, 23, 17, 20, 12, 30, 25

[Note#1: Please write down the numbers from left to right in the below boxes]
[Note#2: Detailed solution gets an additional 1 Mark for this question]

Ans: [5, 9, 12, 17, 20, 23, 30, 25]

Q5) What will be the output at the end of the seventh iteration of selection sort while sorting the
following numbers in ascending order?

9, 5, 23, 17, 20, 12, 30, 25

[Note#1: Please write down the numbers from left to right in the below boxes]
[Note#2: Detailed solution gets an additional 1 Mark for this question]

Ans: [5, 9, 12, 17, 20, 23, 25, 30]

Q5) What will be the output at the end of the fifth iteration of bubble sort while sorting the following
numbers in ascending order?

9, 5, 16, 11, 28, 35, 22, 34

[Note#1: Please write down the numbers from left to right in the below boxes]
[Note#2: Detailed solution gets an additional 1 Mark for this question]

Ans: [5, 9, 11, 16, 22, 28, 34, 35]

Q5) What will be the output at the end of the seventh iteration of bubble sort while sorting the following
numbers in ascending order?

9, 5, 16, 11, 28, 35, 22, 34

[Note#1: Please write down the numbers from left to right in the below boxes]
[Note#2: Detailed solution gets an additional 1 Mark for this question]
Ans: [5, 9, 11, 16, 22, 28, 34, 35]

Q6) Consider the following key values that are inserted into the hash table in the order given using the
hash function H (k) and the hash table is set to 20 from 0 to 19.

96, 48, 63, 29, 87, 77, 48, 65, 69, 94, 61 [1 Mark]

int H(int k) {

int a = (k+7) * (k+7);

a = floor(a/16);

a = a+k;

a = a%20;

return a;

Write down hash index of an element 87, if the hash table uses quadratic probing to hash the given
elements _____

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Answer: 0

Q6) Consider the following key values that are inserted into the hash table in the order given using the
hash function H (k) and the hash table is set to 20 from 0 to 19.

96, 48, 63, 29, 87, 77, 48, 65, 69, 94, 61 [1 Mark]

int H(int k) {

int a = (k+7) * (k+7);

a = floor(a/16);

a = a+k;

a = a%20;

return a;
}

Write down hash index of an element 94, if the hash table uses quadratic probing to hash the given
elements _____

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: 12

Q6) Consider the following key values that are inserted into the hash table in the order given using the
hash function H (k) and the hash table is set to 20 from 0 to 19.

96, 48, 63, 29, 87, 77, 48, 65, 69, 94, 61 [1 Mark]

int H(int k) {

int a = (k+7) * (k+7);

a = floor(a/16);

a = a+k;

a = a%20;

return a;

Write down hash index of an element 65, if the hash table uses quadratic probing to hash the given
elements _____

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: 13

Q6) Consider the following key values that are inserted into the hash table in the order given using the
hash function H (k) and the hash table is set to 20 from 0 to 19.

96, 48, 63, 29, 87, 77, 48, 65, 69, 94, 61 [1 Mark]
int H(int k) {

int a = (k+7) * (k+7);

a = floor(a/16);

a = a+k;

a = a%20;

return a;

Write down hash index of an element 69, if the hash table uses quadratic probing to hash the given
elements _____

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: 11

Q7) Consider an array containing ‘n’ elements. The elements present in an array are in arithmetic
progression, but one element is missing in that order. What is the time complexity to find the position of
the missing element using divide and conquer approach?

A. 𝑂(𝑛)
2
B. 𝑂(𝑛 )
C. 𝑂(𝑙𝑜𝑔 𝑛)
D. 𝑂(𝑛 𝑙𝑜𝑔𝑛)

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: C

Q7) Consider an array ‘A’ containing ‘n’ distinct elements and those elements are in non-decreasing
order. What is the time complexity to find the element in which A[i] = i using divide-and-conquer
approach?

2
A. 𝑂(𝑛 )
B. 𝑂(𝑛 𝑙𝑜𝑔 𝑛)
C. 𝑂(𝑙𝑜𝑔 𝑛)
D. 𝑂(𝑛)
[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: C

Q7) Consider an array consisting of 0’s and 1’s in which all 1’s are followed by all 0’s . What is the time
complexity to count the number of 1’s in an array using divide and conquer?

2
A. 𝑂(𝑛 )
B. 𝑂(𝑛 𝑙𝑜𝑔 𝑛)
C. 𝑂(𝑙𝑜𝑔 𝑛)
D. 𝑂(𝑛)

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: C

Q8) The level-order of Min Heap tree as follows:

4, 23, 14, 32, 53, 63, 40, 87, 50, 90 [1 Mark]

Insert 43, 18, and 2 elements to the above Min Heap tree. Write the Pre-order of the final Min Heap
tree?

[Note#1: No partial marking for the question, hence please kindly carefully enter the node values]
[Note#2: Detailed solution gets an additional 1 Mark for this question]

Ans: Pre-order: 2, 23, 32, 87, 50, 43, 90, 53, 4, 14, 63, 18, 40

Q8) The level-order of Min Heap tree as follows:

4, 23, 14, 32, 53, 63, 40, 87, 50, 90 [1 Mark]

Insert 43, 18, and 2 elements to the above Min Heap tree. Write the Post-order of the final Min
Heap tree?

[Note#1: No partial marking for the question, hence please kindly carefully enter the node values]
[Note#2: Detailed solution gets an additional 1 Mark for this question]
Ans: Post-order: 87, 50, 32, 90, 53, 43, 23, 63, 18, 14, 40, 4, 2

Q8) The level-order of Min Heap tree as follows:

4, 23, 14, 32, 53, 63, 40, 87, 50, 90 [1 Mark]

Insert 43, 18, and 2 elements to the above Min Heap tree. Write the In-order of the final Min Heap
tree?

[Note#1: No partial marking for the question, hence please kindly carefully enter the node values]
[Note#2: Detailed solution gets an additional 1 Mark for this question]

Ans: In-order: 87, 32, 50, 23, 90, 43, 53, 2, 63, 14, 18, 4, 40

Q9) Consider the following elements

3, 6, 7, 5, 4, 1, 0, 9, 8, 10 [1 Mark]

[Note#1: When double rotation is required in order to construct the AVL such as Left-Right (LR) or
Right-Left (RL), should be considered a single rotation]
[Note#2: Detailed solution gets an additional 1 Mark for this question]

How many rotations are required to construct an AVL tree using the above elements? ____

Ans: 6

Q9) Consider the following elements

3, 7, 6, 5, 4, 0, 1, 8, 9, 10 [1 Mark]

[Note#1: When double rotation is required in order to construct the AVL such as Left-Right (LR) or
Right-Left (RL), should be considered a single rotation]
[Note#2: Detailed solution gets an additional 1 Mark for this question]

How many rotations are required to construct an AVL tree using the above elements? ____

Ans: 6
Q9) Consider the following elements

3, 7, 6, 4, 5, 0, 1, 10, 8, 9 [1 Mark]

[Note#1: When double rotation is required in order to construct the AVL such as Left-Right (LR) or
Right-Left (RL), should be considered a single rotation]
[Note#2: Detailed solution gets an additional 1 Mark for this question]

How many rotations are required to construct an AVL tree using the above elements? ____

Ans: 6

Q10) Find the total weight of a minimal spanning tree containing the edges {D, H}, {L, H}, {L, P} in the
given weighted graph by following the kruskal's algorithm.

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: 73
Q10) Find the total weight of minimal spanning tree containing the edges {B, F}, {F, J}, {J, K}, {K, G}, {C,G}
in the given weighted graph by following the kruskal's algorithm.

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: 73
Q10) Find the total weight of a minimal spanning tree containing the edges {A, E}, {E, I}, {I, M} in the
given weighted graph by following the kruskal's algorithm.

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: 73
Q10) Find the total weight of a minimal spanning tree containing the edges {M, N}, {N, O}, {O, P} in the
given weighted graph by following the kruskal's algorithm.

[Note#1: Detailed solution gets an additional 1 Mark for this question]

Ans: 73
Q11) Consider the following digraph starting at vertex 0 (starting discovery time is zero) and apply
Dijkstra’s single source shortest path algorithm on it. Select the correct order from the following in which
vertices are removed from the Priority Queue?

[1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]


A. 0, 1, 2, 3, 4, 5, 6, 7
B. 0, 1, 3, 2, 5, 4, 6, 7
C. 0, 1, 3, 2, 5, 4, 7, 6
D. 0, 1, 3, 2, 4, 5, 7, 6

Ans: C

Q11) Consider the following digraph starting at vertex 1 (starting discovery time is zero) and apply
Dijkstra’s single source shortest path algorithm on it. Select the correct order from the following in which
vertices are removed from the Priority Queue?

[1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]


A. 1, 2, 0, 4, 5, 6, 3, 7
B. 1, 2, 4, 0, 7, 5, 3, 6
C. 1, 2, 0, 4, 3, 5, 6, 7
D. 1, 2, 4, 0, 3, 5, 6, 7

Ans: A

Q11) Consider the following digraph starting at vertex 2 (starting discovery time is zero) and apply
Dijkstra’s single source shortest path algorithm on it. Select the correct order from the following in which
vertices are removed from the Priority Queue?

[1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]


A. 2, 0, 1, 3, 4, 5, 6, 7
B. 2, 0, 1, 4, 5, 6, 3, 7
C. 2, 0, 1, 5, 4, 3, 6, 7
D. 2, 0, 1, 4, 5, 6, 7, 3

Ans: B

Q12) The running time of an algorithm is given by the recurrence relation. Solve the recurrence and
select the correct answer! [1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]


2
A. 𝑂(𝑛 )
B. 𝑂(𝑛 𝑙𝑜𝑔 𝑛)
C. 𝑂(𝑛)
D. 𝑂(𝑙𝑜𝑔 𝑛)

Ans: C

Q12) The running time of an algorithm is given by the recurrence relation. Solve the recurrence and
select the correct answer! [1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]

𝑇(𝑛) = 𝑛 𝑇( 𝑛) + 1000𝑛; T(0)=T(1)=1

2
A. 𝑂(𝑛 )
B. 𝑂(𝑛 𝑙𝑜𝑔 𝑛)
C. 𝑂(𝑛)
D. 𝑂(𝑛 𝑙𝑜𝑔 𝑙𝑜𝑔𝑛)

Ans: D

Q12) The running time of an algorithm is given by the recurrence relation. Solve the recurrence and
select the correct answer! [1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]

𝑇(𝑛) = 𝑛 𝑇( 𝑛 ); T(0)=T(1)=1

2
A. 𝑂(𝑛 )
B. 𝑂(𝑙𝑜𝑔 𝑙𝑜𝑔 𝑛)
C. 𝑂(𝑛)
D. 𝑂(𝑛 𝑙𝑜𝑔 𝑙𝑜𝑔𝑛)

Ans: B
Q12) The running time of an algorithm is given by the recurrence relation. Solve the recurrence and
select the correct answer! [1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]

𝑇(𝑛) = 𝑇( 𝑛) + 1; T(0)=T(1)=1

2
A. 𝑂(𝑛 )
B. 𝑂(𝑙𝑜𝑔 𝑛)
C. 𝑂(𝑛)
D. 𝑂(𝑙𝑜𝑔 𝑙𝑜𝑔𝑛)

Ans: D

Q13) Consider the following program which sorts distinct elements in increasing order

[1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]

for(i=n-1; CONDITION1; i--) {

max = i;
for(j = i-1; CONDITION2; j--) {
if (CONDITION3)
max = j;

swap(A[i] , A[max]);

Which of the following will represent the correct value of CONDITION1, CONDITION2, and
CONDITION3 in the above code?

A. i ≥0; j ≥ 0; A[j] > A[max]


B. i ≥ 0; j ≤ 0; A[j] > A[max]
C. i ≤ 0; j ≥ 0; A[i] < A[max]
D. i ≥ 0; j ≥ 0; A[j] < A[max]

Ans: A
Q13) Consider the following insertion sort algorithm?

[1 Mark]

[Note#1: Detailed solution gets an additional 1 Mark for this question]

INSERTION – SORT (int A[ ], n) {

for(j = 1; j < n; j++) {

Key = A[j];
i = j – 1;
while(i > - 1 && CONDITION1) {
STATEMENT1
i = i – 1;

A[i + 1] = Key;

What is the value of CONDITION1 and STATEMENT1?

A. CONDITION 1: A[i] > Key; STATEMENT 1: A[j] = A[i]


B. CONDITION 1: A[i] > Key; STATEMENT 1: A[i + 1] = A[i]
C. CONDITION 1: A[i] < Key; STATEMENT 1: A[i + 1] = A[i]
D. CONDITION 1: A[i] < Key; STATEMENT 1: A[j] = A[i]

Ans: B

You might also like