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

CS2705 : Programming and Data Structures.

Tutorial 2
Sep 2, 2022

Duration: 1 hour Max. Marks : 15

1. (3 points) An elective course (like CS2705) has students from several departments in the Institute. The
course instructor schedules a one-on-one viva for evaluation. The TA maintains a queue of students to
schedule the viva. At any point, the TA needs to do the following:
• Send the next student for viva to the instructor.
• If a new student arrives for the viva, add the student to the queue.
• Inform the instructor how many students from Mechanical Dept. are currently in the queue.
Modify the standard queue to support these operations. You can assume that a student is identified by
a roll number which is a string. As with IIT M roll numbers, ME dept students are identified by the first
two characters in the roll number string ”ME”.
You are not expected to write any code / pseudo code for this question. State clearly how will you
support these operations. State the running time of each of your operations and the space used by your
data structure, apart from the queue data.

Solution: Idea is to use Queue data structure with an additional counter variable which requires
O(1) space to keep track of the number of students in the Mechanical department. Operation 1
corresponds to enqueue operation and operation 2 corresponds to dequeue operation of the Queue
data structure.
Grading Scheme:

• 1 mark for writing the correct time complexity i.e O(1) for both enqueue and dequeue
• 1 mark for writing how your data structure supports the third operation, that is counting the
number of students from mechanical department.

• 1 mark for the correct space complexity i.e O(1) to support third operation.
• Give only 1 mark out of 3 if the overall idea is correct but time complexity and space com-
plexity of the operations are missing.
• Give only 1.5 marks if the overall idea and time complexity for enqueue and dequeue opera-
tions is correct but the third operation requires O(n) time complexity.

2. (4 points) We have a stack with an interleaved sequence of push and pop operations. The push oper-
ations along with their order is given to us as below. The ... are zero or more pop operations. For the
sake of this question we will assume that pop() operation removes the top element from the stack and
also prints it on the output screen.
push(5), ..., push(4), ..., push(8), ..., push(11), ..., push(3), ..., push(9), ...
Consider the two outputs given below, which are independent of each other:
(a) 8 3 11 9 4 5
(b) 8 11 3 5 4 9
You are expected to fill in the ... with appropriate pop operations to obtain the outputs in (a) and (b).

Solution:

(a) push(5), push(4), push(8), pop(), push(11), push(3), pop(), pop(), push(9), pop(), pop(), pop()
(b) The output sequence cannot be generated with the given sequence of operations.

Grading Scheme:

(a) 2 marks for correct sequence of pop operations. No partial grading.


(a) 2 marks for correct answer. No partial grading.

3. (3 points) Consider the Union-find data structure implemented using singly linked lists with additional
pointers (as discussed in the class) in the following way. If two sets having different sizes are merged,
the “rep pointer” of the elements in the smaller of the two sets is modified. If two sets having the same
size are merged, the “rep pointer” is set to the larger of the two representatives. For example if the
set containing {1} and set containing {2} are merged, the representative of the merged set will be 2.
Consider the sequence of operations given below. Assume that makeset is done for all elements.
union(1, 2), union(3, 4), union(3, 5), find(1), union(1, 7), union(3, 6), union(8, 9), find(2), find(8),
union(1, 8), union(3, 10), find(8).
(a) Write down the answer given by each find operation in the above sequence.
(b) Write down the final sets at the end of the operations with their respective representative elements.

Solution:

(a) find(1)=2, find(2)=2, find(8)=9, find(8)=2.


(b) {3, 4, 5, 6, 10} and {1, 2, 7, 8, 9} with representative elements shown in bold font.

Grading Scheme:

(a) 2 marks for correct answer for find operations. That is 0.5 marks for each correct answer of
find operation.
(b) 0.5 marks for correct set representation and 0.5 marks for correct representative of of each set.

4. Study the function fun which takes as input an singly linked list head. Answer the following questions.

2
bool fun(ListNode* head) {

if (head == NULL || head->next == NULL)


return true;

ListNode *slow = head;


ListNode *fast = head;

while (fast->next && fast->next->next) {


slow = slow->next;
fast = fast->next->next;
}
L1:
ListNode *prev = NULL;
ListNode *curr = slow->next;
ListNode *temp = NULL;

while (curr) {
temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
slow->next = prev;
L2:

ListNode *start = head;


ListNode *mid = slow->next;

while (mid) {
if (start->val != mid->val)
return false;
start = start->next;
mid = mid->next;
}
return true;
}

Given the singly linked list head=[a, b, c, a, c, b, a] answer questions below.


(a) (2 points) Draw the link list above and point to the nodes in the linked list that are pointed to by
slow and fast respectively at point L1.
(b) (1 point) Consider a call to printList(head) at the point L2. What will be printed for the above list?
(c) (1 point) What will be the value returned by fun for the example list given above?
(d) (1 point) For any singly linked list, what does the function fun do? In particular, what is the effect
of the function fun on the list and when does it return true / false?

Solution:

(a) slow= a (4th position), fast= a (7th position).

3
(b) a, b, c, a, a, b, c.
(c) true
(d) Function reverses the second half of the list and check if it matches with the first half. Function
returns true for palindromes.

Grading Scheme:

(a) 1 mark each for specifying the correct position of slow and fast pointer.
(b) 1 mark for the correct answer. No partial grading.

(c) 1 mark for the correct answer. No partial grading.


(d) 0.5 mark for writing that it checks for palindromes and 0.5 marks for writing that it reverses
the second half of the list.

You might also like