Professional Documents
Culture Documents
Tut 2 Sol
Tut 2 Sol
Tutorial 2
Sep 2, 2022
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:
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:
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) {
while (curr) {
temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
slow->next = prev;
L2:
while (mid) {
if (start->val != mid->val)
return false;
start = start->next;
mid = mid->next;
}
return true;
}
Solution:
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.