BRAC UNIVERSITY
A Department of Computer Science and Engineering
Examination: Mid-Term Semester: Spring 2024
Duration: 80 Minutes CSE220: Data Structures Full Marks: 30
Number of Questions: 3 No. of Pages: 3
Name: ID: Section:
(Please write in CAPITAL LETTERS)
✔ Answer all 3 questions. No washroom breaks.
✔ At the end of the exam, put the question paper inside the answer script and return both.
Question 1: CO1 [2 + 8 Points]
I. Suppose you are given a multi-dimensional array with dimensions 4x5x4. What is the
linear index of the multidimensional index [2][1][0]?
II. Suppose you're working as a cryptographer for a secret intelligence agency. You are
given an encrypted matrix as an input. You have to decrypt it after some processing of
this matrix.
To decrypt this message efficiently, you have a task to construct a method called
sum_diff(matrix) which takes an encrypted matrix as input and returns a decrypted
linear array. The process of finding the decrypted linear array is given below:
You have to find out the column-wise summations for each column and store the
difference of subsequent column-wise summations in a new linear array.
Sample Input Sample output Explanation
Sum of 0th column = 29
Sum of 1st column = 16
1 3 1 -13 1
Sum of 2nd column = 17
6 4 2
Therefore, the size of the
5 1 7 resulting array is 2 and the
array is:
9 3 3
16-29 = -13 17-16 = 1
8 5 4
Python Notation Java Notation
def sum_diff(matrix): public int[] sum_diff(matrix) {
# To Do // To Do
}
Question 2: CO5 [10 Points]
You are given a non-dummy headed singly linear linked list containing positive integers. Your
task is to complete the given method IsSumPossible() which takes the head of the linked list
and an integer, n as input. If the integer n can be obtained by summing any two of the elements
of the linked list, return True, else return False.
Page 1 of 3 Set-A
Hint: There might be more than one such pair possible, you don’t have to check all such
pairs.
[DO NOT USE OTHER DATA STRUCTURE OTHER THAN GIVEN LINKED LIST]
Sample Input Returned Value Explanation
list = 12345 True Sum of the elements (3,4) or (2,5) equals
n=7 7
list = 12456 False There are no two elements that make the
n=4 sum 4. Note that though there is an
element 4 itself in the list, the output will
be False.
list = 5 False There is only one element so, the sum of
n=5 two elements cannot be 5
Python Notation Java Notation
def IsSumPossible(head, n): public boolean IsSumPossible(Node head, int n) {
# To Do // To Do
}
Question 3: CO3 [2+8 Points]
I. Suppose that an intermixed sequence of stack push and pop operations are performed.
The pushes push the integers 0 through 9 in order; the pops print out the return value.
Which of the following sequences could not occur for push and pop operation? Write all
the correct answers.
A. 1 2 3 4 5 6 9 8 7 0
B. 0 4 6 5 3 8 1 7 2 9
C. 1 4 7 9 8 6 5 3 0 2
D. 2 1 4 3 6 5 8 7 9 0
II. You are given a Stack of people. They should be paired off as man with woman, woman
with man. If a male and a female are consecutive in the stack, they get paired
immediately. If there are consecutive females found without any subsequent males, then
the spare females should be saved in a Queue for future pairing. If there are
consecutive males found without any subsequent females, we try to pair a male with the
first spare female [if available] from the Queue. If no spare female is available, then the
male is discarded. The same logic applies to consecutive females without any
subsequent males.
The stack class and the Queue class are implemented for you. The public instance
methods in the Stack class are push(), pop(), peek(), and isEmpty(). The public
instance methods in the Queue class are enq(), deq(), peek(), and isEmpty(). In
both classes, isEmpty() returns a boolean value, peek() and pop() returns None for
underflow. All other members of both classes are private. You can only use the public
instance methods. No need to consider the overflow exception for both Stack and Queue
classes.
Your task is to complete the given method dance_pair() that takes a stack and prints
every male-female pair.
Page 2 of 3 Set-A
Every male and female are written in this way - M_id or F_id. A method called
id_gender_extractor(s) is implemented for you that returns the id and gender from a
string,s.
You do not need to write the methods/classes that are implemented for you. You
cannot use any data structure other than stack and queue.
Sample Input Output Explanation
st = 10 and 20 are paired M_10 and F_20, F_5 and
together M_3, M_7 and F_9 are
To M_10
5 and 3 are paired sequential in the stack.
F_20 together They are paired up.
4 and 19 are paired
F_4 together From F_4, there are
7 and 9 are paired consecutive females. So,
F_5 together F_4 is stored in the queue
for future pairing.
M_3
From M_19, there are
M_19 consecutive males. M_19
is paired with spare F_4.
M_1 M_1 is discarded since
there is no spare female
M_7
with whom M_1 can be
F_9 paired.
F_18
Given:
def id_gender_extractor(s): Driver Code:
if s != None: st = Stack()
return s[0],s[2:] #gender, id st.push('F_18')
else: st.push('F_9')
return None, None st.push('M_7')
st.push('M_1')
st.push('M_19')
st.push('M_3')
st.push('F_5')
st.push('F_4')
st.push('F_20')
st.push('M_10')
dance_pair(st)
Python Notation Java Notation
def dance_pair(st): public void dance_pair(Stack st) {
# To Do # To Do
}
Page 3 of 3 Set-A