Midterm Exams
Midterm Exams
1st Midterm
26.10.2015
Question 1 2 3 4 5 6 7 8 Total
Points 100
Grade
Question 1.
Mark the following statements as True or False. Correct the false statements with a
concise explanation.
(a) A data structure is a way to store and organize data in computer, so that it can
be used easily.
(b) Bottom-up and top-down analyses are two approaches in the system’s
life cycle.
(c) Big-Oh (O) notation gives a lower bound on the running time of a
program.
(d) 3 n^2 + 10 n log n = Omega(n^2)
(e) 3 n^2 + 10 n log n = O(n log n)
Question 2.
Let an array definition be:
int a[5][5][5][5];
Question 3.
Let a[n][n] be an upper triangular matrix (see the example given below). The
elements of this triangular matrix are stored in a one-dimensional array as given
below:
a a a a
oo o1 o2 o3
0 a a a
11 12 13
0 0 a a
22 23
0 0 0 a
33
1
U
a00 a10 a11 a20 a21 a22 a30 a31 a32 a33
Please complete the method readtriangularmatrix(int[], int) that reads integers from
the keyboard and fills the one-dimensional array U with an upper triangular matrix.
int i, j, k;
if(n*(n+1)/2 > MAX_SIZE){
printf(“\n invalid array size \n“);
exit(-1);
}
else
for(i=0; i<=n-1; i++){
k=……………………………..
for(j=0; j<=i; j++)
scanf(“%d“, ………………….);
}
}
Question 4.
Question 5.
2
3
BBM201 – Data Structures – Fall 2015
2nd Midterm
30.11.2015 – 9:30-11:20
Name Surname:
Student ID : Section:
Duration: 100 minutes
Question 1 2 3 4 5 6 7 8 9 10 * Total
Points 10 9 10 7 12 8 12 12 10 10 10 100
Grade
Question 1. Decide if the following statements are true or false, circle your answer.
Give a short explanation if you chose ‘False’.
a) The worst-case cost to insert an element to the beginning of a linked list of size n is
O(1).
True ☐ False ☐
b) The worst-case cost to insert an element to the end of an array of size n is O(n), if
there is still space at the end of the array.
True ☐ False ☐
It is O(1) because there is no shifting. We only add the item to the given address.
c) The worst-case cost to delete an element from a linked list of size n is O(1).
True ☐ False ☐
It is O(n) because first we need to find the item to be deleted by traversing the
linked list.
e) The memory used to store a linked list of 5 integers is 40 bytes (suppose each
pointer is 4 bytes).
True ☐ False ☐
1
Question 2. A palindromic word is a sequence of characters that reads the same
backward and forward. For example; pepper, refer, kayak are palindromic words. The
method IsPalindrome(char[] str) checks if a given string is palindrome by using a
stack. According to the given prototypes of stack methods, please fill in the gaps in
the code.
int stack[10];
int top=-1;
void push(char); //pushes the given char on the stack.
char pop(); //pops a char from the stack.
if (count==len)
printf("\n%s is a Palindrome string\n", str);
else
printf("\n%s is not a palindrome string\n", str);
We can evaluate a postfix notation by using only one stack, whereas in prefix
notation we have to use at least two stacks for the evaluation. In addition, we
need to traverse to the end of the prefix notation and then we need to calculate
the expression backwards.
2
Question 4. Let 48+2*35-+47-* be an expression given in postfix notation
(suppose each number has only one digit again). Fill the table on the right
accordingly, if the input is stored and processed using a stack notation in order to
evaluate the given expression. ([0],[1],[2] indicate the position number of an element,
[0] being the bottom position.)
Stack
Token
Top
[0] [1] [2]
4 4 0
8 4 8 1
+ 4+8 0
2 4+8 2 1
* (4+8)*2 0
3 (4+8)*2 3 1
5 (4+8)*2 3 5 2
-
(4+8)*2 3-‐5 1
+
((4+8)*2)+(3-‐ 0
5)
((4+8)*2)+(3-‐ 1
4 4
5)
((4+8)*2)+(3-‐ 2
7 4 7
5)
((4+8)*2)+(3-‐ 1
- 4-‐7
5)
*
(((4+8)*2)+(3-‐ 0
5))*(4-‐7)=-‐66
3
Question 5. Multiple stacks/queues have a recovery mode if it is full and needs to
add more items. The main idea is finding another stack/queue that has a space, which
we can use by shifting the items. A sample multiple circular queue is given below
where there are 5 circular queues of size 4. Queue 0 and Queue 2 are full and others
are not. For each queue a front and a rear pointer are kept.
Explain the steps that need to be taken when adding to a full queue and take recovery
steps. Bear
in
mind
that
there
are
different
situations
of
a
full
queue.
Use short
sentences. No coding is required.
If the rear comes after the front look at the right of the full queue in the multiple
queue structure. If there is any space in any queue on the right, shift the items
one space to the right. If the front comes after the rear, check if there is any
space in any queue on the left, shift the items one space to the left to have one
space for the new item.
((1-‐(4/(3*(1+2))))+(3*(5-‐2)))/2
/+-‐1/4*3+12*3-‐522
4
Question 7. According to the given array based linked list which is defined as
follows:
#define MAX_LIST 10
typedef struct{
char letter;
int link;
}item;
item linkedlist[MAX_LIST];
int free_; //the index of the first free space in the list
int* list; //the index of the first item in the list
Imagine we begin with a sorted list given in the first table given below. After we
insert “C” and delete “A” in the sorted list, what will be the contents of the list?
Please fill in the tables and write down the values of free_ and *list.
5
Question 8. Given an unsorted linked list, and without using any extra memory, fill in
the blanks in the below method that will delete any duplicates from the linked list.
(Brute force solution is acceptable.)
6
Question 9.
Write a recursive method that searches for a value in a linked list and returns the
position of the value in the linked list if found, otherwise returns -1. The first item in
the linked list has position 1. You can add more parameters for the given method, if
needed. (Using global variable is not allowed!)
Search “1” will
return 3
3 6 1 2 NULL
head
struct Node{
int data;
struct Node* next;
};
if(head==null)
return -1;
else if(head->data==value)
return count+1;
else
SearchItem(head->next,value,index+1)
}
7
Question 10. Print the contents of the linked list returned by the Func function for the
provided two linked lists and the code below.
list3-> 0à1à2à3à4à5à6à7àNULL
Node* newhead,temp;
if (list1->data < list2->data) {
newhead = list1;
list1= list1->next;
}
else {
newhead = list2;
list2 = list2->next;
}
temp = newhead;
8
Bonus Question. Consider the sorted linked list with these nodes:
NULL
10 40 60 80
ptr1
ptr2 50
The only pointer that is given for this list is ptr1 that is pointing to the node with the
value 60 (shown above) and there is no head pointer provided. There is also a new
node with the value 50 and a pointer, ptr2, pointing to it.
Write the steps for inserting the new node to the above list in the sorted order. Explain
your answer in sentences. No coding is required.
9
BBM201 – Data Structures – Fall 2015
Make-up Exam
28.12.2015 – 13:00
Name Surname:
Student ID : Section:
Duration: 60 minutes
Question 1 2 3 4 5 Total
Points 24 12 14 30 20 100
Grade
Question 1.
function choose(n,r)
for i:=0 to n-r do T[i,0]:=1;
for i:=0 to r do T[i,i]:=1;
for j:=1 to r do
for i:=j+1 to n-r+j do
T[i,j]:=T[i-1,j-1]+T[i-1,j]
return T[n,r]
1
b) The following code, called binsearch, searches a given number (searchnum) in a
sorted list of n numbers. What is the worst-case, average-case and best-case running
time of this algorithm in terms of n? Explain your answer.
Question 2. What is the output of the following code block if the stack consists of
items {1,3,5,6,9}, where 1 is the top?
/*-6/7+5/3+1*24-15-28
a) Infix notation:
b) Postfix notation:
Question 4. Fill the below Reverse function which reverses given singly linked
list using recursion.
//global variables
struct Node * head;
…
void main(){
…
Reverse(head);
…
}
}
Question 5. According to the given array based linked list which is defined as
follows:
#define MAX_LIST 10
typedef struct{
char name[7];
int link;
}item;
item linkedlist[MAX_LIST];
int free_; //the index of the first free space in the list
int* list; //the index of the first item in the list
name link
[0] “Banu” 1
[1] “Irem” 4
[2] “Zerrin” -1
[3] “Ali” 0
[4] “Leyla” 6
[5] “Ahmet" 3
[6] “Mehmet” 2
[7] 8
[8] 9
4
[9] -1Imagine we begin with a sorted list given in the
first table given below. After we insert “Adil” and
delete “Leyla” in the sorted list, what will be the
contents of the list? Please fill in the tables and write down the values of free_ and
*list.
[0] [0]
[1] [1]
[2] [2]
[3] [3]
[4] [4]
[5] [5]
[6] [6]
[7] [7]
[8] [8]
[9] [9]
5
BBM201 – Data Structures – Fall 2016
1st Midterm
11.11.2016
Name Surname:
Student ID : Section:
Süre … dakikadır.
Question 1 2 3 4 5 6 7 8 Total
Points 100
Grade
Question 1. Decide if the following statements are true or false, circle your answer.
Give a short explanation if you chose ‘False’.
a) A data structure is a way to store and organize data in computer, so that it can
be used efficiently.
True ☐ False ☐
True ☐ False ☐
b) Big-Oh (O) notation gives a lower bound on the running time of a program.
True ☐ False ☐
c) The cost of insertion (push) and deletion (pop) of an element in a stack is O(1).
True ☐ False ☐
d) The complexity of the worst-case running time of binary search for an array of size
n is n2.
True ☐ False ☐
1
Question 2. A palindromic word is a sequence of characters that reads the same
backward and forward. For example; repaper, refer, kayak are palindromic words.
The method IsPalindrome(char[] str) checks if a given string is palindrome by using a
stack. According to the given prototypes for stack operations, please fill in the gaps in
the code.
void push(char);
void pop();
if (____________________)
count == len
printf("%s is a palindromic string\n", str);
else
printf("%s is not a palindromic string\n", str);
}
Question 3.
Question 4.
2
Question 5.
Let a[n][n] be an upper triangular matrix (see the example given below). The
elements of this triangular matrix are stored in a one-dimensional array as given
below:
a a a a
oo o1 o2 o3
0 a a a
11 12 13
0 0 a a
22 23
0 0 0 a
33
U
a00 a10 a11 a20 a21 a22 a30 a31 a32 a33
Please complete the method readtriangularmatrix(int[], int) that reads integers from
the keyboard and fills the one-dimensional array U with an upper triangular matrix.
int i, j, k;
if(n*(n+1)/2 > MAX_SIZE){
printf(“\n invalid array size \n“);
exit(-1);
}
else
for(i=0; i<=n-1; i++){
k=……………………………..
for(j=0; j<=i; j++)
scanf(“%d“, ………………….);
}
}
Question 6.
If C is the array shown with its address above each node, write what the following
lines of a program will print in the empty column.
int C[3][2][2];
3
printf("C=%d", C);
printf("C+2=%d", C+2);
printf("*(*C+1)) =%d", *(*C+1));
printf(" *(C[0]+1)=%d ’’, *(C[0]+1)) ;
printf(" *(C[2][1]+1)=%d ’’, *(C[2][1]+1));
printf(" *(*((*(C+2))+1))=%d ’’, *(*((*(C+2))+1));
4
BBM201 – Data Structures – Fall 2017
1st Midterm
02.11.2017 – 90 minutes
Name Surname:
Student ID :
Section: ☐ Section 1 (Burcu Can)
☐ Section 2 (Sevil Şen)
☐ Section 3 (Adnan Özsoy)
Questions 1 2 3 4 5 6 Total
Points 15 16 14 14 21 20 100
Grade
2121
b. Please write the recursive method that applies exponentiation on a given integer x,
so it returns xm. Global variables are not allowed for the solution. (10 points)
if (m == 0)
return 1;
return x * exp(x, m-1);
1
2. (Stack/Queue) Given the initial empty position of stack and queue (circular) below,
give the final representation of data below for array representations and fill the values
of top, front and rear positions into an array of size 8. (16 points)
a. Stack (8 points)
Push (5) , Push (2) , Pop () , Push (6) , Push (3) , Pop () , Push (8) , Push (2) , Push (7)
, Push (4) , Push (6) , Push (3) , Push (8)
0 1 2 3 4 5 6 7
5 6 8 2 7 4 6 3
Top
Initial -1
Final 7
0 1 2 3 4 5 6 7
1 6 3 8 2 7 4
First Rear
Initial -1 -1
Final 2 0
2
3. (Pointers) If C is the array shown with its address above each node, write what the
following lines of a program will print in the empty column. (14 points)
116 132
3 4 1 5 0 2 9 8 7 2 10 12
int C[3][2][2];
a. 5n2 – 6n = O (n2)
c. n3 + 106n2 = O (n3)
d. nk + n + nklogn = O (nklogn)
3
5. (Problem Solving) Write a C function to find out the maximum and second
maximum numbers from a given two-dimensional array of integers. Complete the
code below where there are spaces “______” as needed and inside the function
TwoMax. Your answer should trace the array only once for full credit. (21 points)
int main()
{
int first=0, second=0;
int **array;
return 1;
}
int i, j;
*first = *second = arr[0][0];
for(i = 0; i <n ;i++)
{
for(j = 0; j <m ; j++){
4
6. (Sparse Matrix) A matrix is called symmetric if for all values of i and j satisfies
𝑚𝑚
A[i][j] = A[j][i]. A sparse matrix has at least 2 + 1 zero values out of all m items in
the matrix. Given that a matrix A is sparse, symmetric and square (NxN) (20 points)
Use the same representation as sparse matrix, except show only lower
triangle(or upper) non zero values
Since sparse has at most m/2 non zero, and we will only show half of the
non zero values, soin maximum we will use m/4 values, using 3 pointers
each, thus 3m/4 is better than m
5
BBM201 – Data Structures – Fall 2017
2nd Midterm
14.12.2017 – 13:00-15:00
Name Surname:
Student ID : Section:
Duration: 120 minutes
Question 1 2 3 4 5 6 7 Total
Points 12 12 16 18 12 12 18 100
Grade
aa+bc-* = 100
b. Convert the following fully parenthesized infix expression to postfix and prefix notation:
(x-((x+y)*((z/r)+p)))
postfix : xxy+zr/p+*-
prefix : -x*+xy+/zrp
Question 2. Trees. Give the traversal order of the accessed elements for each traversal algorithm for the given
tree.
Preorder: FBADCEGIH
Inorder: ABCDEFGHI
Postorder: ACEDBHIGF
Question 3. Array based linked list. According to the given array based linked list that is defined as follows:
#define MAX_LIST 10
typedef struct{
char name[7];
int link;
}item;
item linkedlist[MAX_LIST];
int free_; //the index of the first free space in the list
int* list; //the index of the first item in the list
Imagine we begin with a sorted list in the first table given below. After we insert “Adil” and delete “Leyla”
successively in the same sorted list, what will be the contents of the list? Please fill in the tables and write
down the values of free_ and *list.
Question 4. Linked lists. The information of athletes applying for a long distance run will be stored as a linked
list. Assume that the athelete list is not globally defined.
#define ARRAY_SIZE 21
a. Write the function named printList that displays all the information of the athletes whose running
times are smaller than the time given as a parameter to the function.
b. Write the function named append that adds a new athlete with the given no, name and time
information to the end of the list.
}
c. Complete the main method given below according to the above implementations.
#define TABLE_SIZE 6
ptr->key = key;
ptr->link = NULL;
if (trail) trail->link = ptr;
else hash_table[hash_value]= ptr;
}
According to the above hashtable implementation, fill in the below figure after the following method calls are
applied:
6 12 0
1 7
20
9 3
5
Question 6: Doubly linked list.
4 7 3 9 4 7 6 3 9
head
head 6 pre_node pre_node
new_node
Write the method that inserts a new node before the given previous node in a doubly linked list. An example
situation is given below.
struct node{
int data;
struct node* next;
struct node* pre;
};
void insert(struct node* pre_node, struct node* new_node){
new_node->next = pre_node;
new_node->pre=pre_node->pre;
pre_node->pre->next = new_node;
pre_node->pre = new_node;
Question 7. Binary trees. We will use an array in order to store a full binary tree.
3 7
1 9 2 4
a) Fill in the given array that will include the tree contents given above.
5 3 7 1 9 2 4
b) Complete the given code that returns the nth node in the given level. For example, find_data(3, 2) will
return 9, find_data(2, 1) will return 3, and so on.
#define ARRAY_SIZE 7
int tree[ARRAY_SIZE];
int pow (int x, int y); //assume the method is already defined.
}
BBM 201
Exam 1
Time: 60 minutes
Name:
1. (15 points) If B is the array shown with its address above each node, write what the
following lines of a program will print.
int B[2][3];
2. (12 points) Provide the time complexities below for the worst-case.
(a) (4 points) Write the time complexity of inserting and deleting an element of an
array.
(b) (4 points) Write the time complexity of inserting and deleting an element of a
linked-list.
(c) (4 points) Write the time complexity of push(x), pop(), top() and IsEmpty() oper-
ations for an element x of a stack.
3. (9 points) How many bytes do the following data structures occupy in the memory?
(a) (3 points) An INTEGER array of size 5.
(b) (3 points) A CHARACTER doubly linked-list of size 5.
(c) (3 points) A DOUBLE linked-list of size 5.
4. (12 points) (a) (6 points) Show how a stack looks after each of the following operations
is applied consecutively. Write your answer in the stack column of the table below.
Assume that this stack is initially empty.
(b) (6 points) Write what IsEmpty( ) and Top( ) would return for the current stack
if it was executed after each operation in the table below.
Page 2
5. (16 points) (a) (12 points) Write each of the recursive calls made in their order when
Abc(head) is executed in main (). The address of each node is written above that
node in this linked list.
(b) (4 points) Write what main() will return in this program program.
(Note: Insert(head, data) inserts a new node in the beginning by putting data
as the value of the head node.)
Page 3
6. (36 points) The Reverse function below reverses any given linked list by using iteration
method. The input argument of the Reverse function is the head of the linked list. In the
following linked list, the number above each node indicates its address in the memory.
(a) (20 points) If we execute the Reverse function for this linked list, redraw the
changes on the input linked list after EACH ITERATION STEP of the while loop
by showing the changes on the NODES and on the LINKS (arrows).
(b) (16 points) Write the value of addresses stored in “current”, “prev” and “next”
initially and after each iteration step of the while loop in the table below.
Page 4
BBM 201 – Data Structures - Fall 2019
1st Midterm
November 19, 2019
Name: ________________________________________________________________
INSTRUCTIONS
• Do not open this exam booklet until you are directed to do so. Read all the
instructions first.
• When the exam begins, write your name on every page of this exam booklet.
• The exam contains six multi-part problems. You have 120 minutes to earn 105
points (5pts bonus).
• The exam booklet contains 6 pages including this one.
• This exam is an open book and notes exam. You are allowed to have two pieces of
bound books or notebooks.
• Please write your answers in the space provided on the exam paper.
• Be neat.
• Good luck!
QUESTIONS
a) (3 pts) For a collection of algorithms that runs in O(1), O(n log n), O(n), O(n 2),
O(log n), O(n!), order the algorithms from fastest to slowest.
b) (12 pts) Find the big-O time complexity of each of the following code fragments.
Hint:
(𝑛 − 1)(𝑛)
1 + 2 + 3 + ⋯ + (𝑛 − 2) + (𝑛 − 1) = = 𝑂(𝑛2 )
2
𝑛 𝑛
1+2+4+⋯+ + + 𝑛 = 2𝑛 − 1 = 𝑂(𝑛)
4 2
int i = 1; int i = n;
while (i <= n) { while (i > 0) {
print("*"); for (int j = 0; j < n; j++)
i = 2 * i; print ("*");
} i = i / 2;
}
a) (12 pts) Write a recursive function (that takes a string and the length of the
string as arguments) in C that returns 1 if the given string is palindrome, and 0 if
it is not. A string is said to be a palindrome if the string read from left to right
is equal to the string read from right to left. For example, "kayak", "level", "radar",
"repaper", "noon" are all palindrome words in English, however "paper", "book",
"little" are not. Only recursive solutions will be accepted.
}
void recursiveFun(int value)
{
if(0 < value && value < 10)
{
recursiveFun(value – 2);
recursiveFun(value + 1);
printf(“ %d”, value);
}
}
}
192
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Let A[n][n] be a lower triangular matrix (see the example given below in A and C). The
elements of this triangular matrix are stored in a one-dimensional array (U). We want
to convert the given left-aligned lower triangular matrix into a right-aligned lower
triangular matrix as given in B and D without changing the order of the items in the
one-dimensional array U.
gettriangularmatrix(int i,int j,int n) method returns the item in the given index
(A[i][j]).
Which lines in the method should we change to make it work for the right-aligned lower
triangular matrix for the same one-dimensional array? Please write the updated lines
on the side of each line that needs to be changed. No new lines will be added. Leave
the space blank for the lines which do not require any changes.
A B
a 0 0 0 0 0 0 a
00 03
a a 0 0 0 0 a a
10 11 12 13
a a a 0 0 a a a
20 21 22 21 22 23
a a a a a a a a
30 31 32 33 30 31 32 33
C D
1 0 0 0 0 0 0 1
2 3 0 0 0 0 2 3
4 5 6 0 0 4 5 6
7 8 9 10 7 8 9 10
U
a00 a10 a11 a20 a21 a22 a30 a31 a32 a33
U
a03 a12 a13 a21 a22 a23 a30 a31 a32 a33
if(i<0||i>=n||j<0||j>=n){ ...................................... 1
printf(“\n invalid index\n“);............................ 2
exit(-2);
}
else if(i>=j) //valid index ........ else if (i+j>=n-1)..... 3
return (i+1)*i/2+j ........... (i*(i+1)/2)+j-(n-i-1)...... 4
else return -1; ....................................... 5
}
Question 5) Stacks (20 pts):
Answer the following questions by putting a check mark on the correct column. Unless
otherwise stated, you have access to only the stack interface functions. You cannot
directly access the underlying array structure.
b) (10 pts) For a given integer queue Q, fill in the blanks in the below pseudocode
so that the function findMaxOfQueue will find the maximum element in Q. You may only
use the operations: enqueue(), dequeue(), size().
Usage:
dequeue() : Dequeues an item from Q
enqueue(X) : Enqueues X into Q
size() : Returns the size of Q
Queue must remain intact after finding the max. Each blank is either a complete single
line or part of a single line.
findMaxOfQueue (Queue Q)
{
max = ……………… dequeue()……………………… ;
…………………………enqueue (max) ………………………… ;
for ( int i=1 ; ……i<size()…………… ; ………i++……… )
{
next = ……………… dequeue()……………;
if (………………next>max………………………) {
…………………max=next…………………… ;
}
………………… enqueue(next)…………………… ;
}
return ……………………max………………… ;
}
BBM 201 – Data Structures - Fall 2019
2nd Midterm
December 24, 2019
Name: ________________________________________________________________
INSTRUCTIONS
• Do not open this exam booklet until you are directed to do so. Read all the instructions
first.
• When the exam begins, write your name on every page of this exam booklet.
• The exam contains six multi-part problems. You have 120 minutes to earn 105 points (5pts
bonus).
• The exam booklet contains 7 pages including this one.
• This exam is an open book and notes exam. You are allowed to have two pieces of bound books
or notebooks.
• Please write your answers in the space provided on the exam paper.
• Be neat.
• Good luck!
QUESTIONS
(10) a. Convert the following fully parenthesized infix expression to postfix and prefix
notations:
(x-((x+y)^((z/w)-q)))
postfix: xxy+zw/q-^-
prefix: -x^+xy-/zwq
(10) b. Fill in the table below accordingly to convert the infix expression
(E-F)^A/C
to a postfix expression using an operator stack. Note that you are supposed to leave some cells
blank (In other words, you should not fill all blank cells).
According to the given array based linked list which is defined as follows:
#define MAX_LIST 10
typedef struct{
char name[10];
int link;
}planet;
planet linkedlist[MAX_LIST];
int free_; //the index of the first free space in the list
int* list; //the index of the first item in the list
Imagine we begin with an alphabetically sorted list in the first table given below. In the sorted
list, each element holds a link to the next element. That link points to the index of the next
element. The first element in the list can be accessed through *list. The first available element
in the list, where a new element can be inserted, is accessed through free_. The available items
are also linked to each other in the same way.
After we insert “Jupiter” and delete “Neptune” successively (you will delete “Neptune” after you
insert “Jupiter”) in the same sorted list, what will be the contents of the list? Please fill in
the tables and write down the new values of free_ and *list.
(10) a. Complete the given method that returns 1 if the given linked list is circular,
otherwise returns 0. Please note that an empty linked list is supposed to be circular. You are
allowed to define only an extra variable without doing any memory allocation.
struct node
{
int data;
struct node* next;
};
if (head==null)
return 1;
(10) b. This time write a recursive method that returns 1 if the given linked list is circular,
otherwise returns 0. Again an empty linked list is supposed to be circular. The parameter ‘cur’
points to the head node in the first call. You are not allowed to define any other variable.
struct node
{
int data;
struct node* next;
};
if (head==null)
return 1;
if(cur==null)
return 0;
if(head==cur)
return 1;
}
Question 4) Array of Linked Lists (15 pts):
For the program code given below, input of func is:
Hint: (int)log10(94634) = 4
(5) a. Write down the exact output line printed after == PART 1 : ==
2 >> 0 >> 3 >> 0 >> 1 >> 1 >> 0 >> 0 >> 0 >> 0 >>
(10) b. Write down the exact output line printed after == PART 2 : ==
>> 7 >> 2 >> 357 >> 235 >> 812 >> 45645 >> 545678
co[om]++;
if (newHeads[om] == NULL) {
newHeads[om] = temp;
newTails[om] = temp;
}
else {
newTails[om]->next = temp;
newTails[om] = temp;
}
temp = temp->next;
}
int i = 0;
for( ; co[i] == 0; i++);
*A = newHeads[i];
tail = newTails[i];
temp = *A;
printf("\n== PART 2 : ==\n");
for( ;temp != NULL; temp = temp->next)
printf(" >> %d",temp->element);
printf("\n************** \n");
}
Question 5) Linked List Implementation of Stack (19 pts):
Fill the following empty lines to implement a recursive function to find the minimum value stored
in a linked list-based stack. You can assume that the stack is not empty. Each empty line is to
be filled with one of the lines on the right. Only 6 lines from the right will be used. Write
the corresponding line number inside the parenthesis.
Now implement the same recursive function assuming you have no access to the underlying linked
list structure. You can only use the standard stack functions to access and modify a global
stack. You can assume that the stack is not empty. Each empty line is to be filled with one of
the lines on the right. Only 7 lines from the right will be used. Write the corresponding line
number inside the parenthesis.
int FindMin() 1 pop();
{
( 9 ) 2 push(a);
( 1 )
( 7 ) 3 return minVal;
(10 )
4 minVal = FindMin();
( 4 )
( 2 ) 5 if(isEmpty())
( 8 )
} 6 push(minVal);
10 if(!isEmpty())
Question 6) Circular Linked List Implementation of Stacks and Queues (16 pts):
Consider a dynamically allocated linked list based circular data structure. As long as there are
free nodes available, new node requests are answered by these nodes. The last node with a value
is followed by the first free node and the last free node is followed by the first node with a
value as shown in the figure below:
This structure can be implemented in many different ways based on the following possible pointers
and/or values:
- store a pointer, pfV, to the first node with a value (top, front)
- store a pointer, pfF, to the first free node
- store a pointer, plV, to the last node with a value
- store a pointer, plF, to the last free node
- number of values stored in the list, nV
- number of free nodes, nF
Assuming that there are m nodes with values and k free nodes in the list (m>1, k>1), fill in the
following table with the number of nodes accessed or modified (value or link fields are read or
changed) during each ADT operation for the corresponding implementation variant, assuming a smart
implementation. You can assume that the free nodes can be rearranged/reordered as necessary, but
the nodes with values maintain their order at all times.
Q1 Academic Honesty
0 Points
Q2 Complexity
10 Points
Q2.1
2 Points
! O(n)
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 1 of 11
View Submission | Gradescope 16.12.2020 20:59
! O(n/2)
" O(log2 n)
! O(n log2 n)
Q2.2
2 Points
n3 + 106 n2
" O(n3 )
! O(n2 )
! O(106 n2 )
! O(106 )
Q2.3
2 Points
! O(n2 )
! O(n2 logn)
" O(n3 )
! O(n2 /2)
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 2 of 11
View Submission | Gradescope 16.12.2020 20:59
! O(n2 /2)
Q2.4
2 Points
nk + n + nk logn
! O(nk )
" O(nk logn)
! O(nk + n)
! O(nk + nk logn)
Q2.5
2 Points
! O(n2 )
! O(n2 logn)
" O(nlogn)
! O(logn)
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 3 of 11
View Submission | Gradescope 16.12.2020 20:59
Q3 Recursion
10 Points
18
Q4 Multidimensional Array
20 Points
Q4.1
10 Points
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 4 of 11
View Submission | Gradescope 16.12.2020 20:59
Q4.2
10 Points
Q5 Struct/Stack/Tree
12 Points
Q5.1
2 Points
Q5.2
2 Points
Q5.3
4 Points
Q5.4
4 Points
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 6 of 11
View Submission | Gradescope 16.12.2020 20:59
Q6 Queue
10 Points
Q6.1
5 Points
Note: For your answer write each content of the array from left
to right with spaces, and if there is a gap put - so if the array has
empty places at index 3, 6, and 7 then a sample output will be:
123-45--
1-638274
Q6.2
5 Points
20
Q7 Data Structure
20 Points
Example:
D = (d1 , d2 , d3 ) where
Td1 =(computer, engineer, data, structures, analysis)
Td2 =(computer, science, database)
Td3 =(computer, science, AI, data, algorithms)
T =(AI, algorithms, analysis, computer, data, database,
engineer, science, structures)
The query Q =(computer, data) returns (d1 , d3 ).
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 8 of 11
View Submission | Gradescope 16.12.2020 20:59
Q8 Binary Tree
18 Points
For each of the below traversal algorithms, give the order of the
nodes that are visited for the below tree structure.
Write the solution as a space-separated string e.g., 1 2 3 4 5 6 7
8
Q8.1
6 Points
Preorder:
12457368
Q8.2
6 Points
Inorder:
42751386
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 9 of 11
View Submission | Gradescope 16.12.2020 20:59
Q8.3
6 Points
Postorder:
47528631
STUDENT
TOTAL POINTS
77 / 100 pts
QUESTION 1
QUESTION 2
Complexity 10 / 10 pts
2.1 (no title) 2 / 2 pts
QUESTION 3
Recursion 10 / 10 pts
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 10 of 11
View Submission | Gradescope 16.12.2020 20:59
QUESTION 4
QUESTION 5
Struct/Stack/Tree 12 / 12 pts
5.1 (no title) 2 / 2 pts
QUESTION 6
Queue 10 / 10 pts
6.1 (no title) 5 / 5 pts
QUESTION 7
QUESTION 8
https://www.gradescope.com/courses/202139/assignments/852419/submissions/57214107 Page 11 of 11
8.12.2021 10:38 View Submission | Gradescope
Q1
0 Points
Yasin Şimşek
Q2
8 Points
Q2.1
2 Points
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 1/15
8.12.2021 10:38 View Submission | Gradescope
Q2.2
2 Points
isPalindrome(s)
isPalindrome(s, low, high)
isPalindrome(s, low + 1, high)
isPalindrome(s, low, high - 1)
isPalindrome(s, low + 1, high - 1)
Q2.3
2 Points
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 2/15
8.12.2021 10:38 View Submission | Gradescope
3 33
11
56
5 33
33 5
Q2.4
2 Points
Q3
10 Points
Propose a data structure that supports the stack push and pop
operations and a third operation findMin, which returns the smallest
element in the data structure, all in O(1) worst-case time.
Q4
20 Points
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 3/15
8.12.2021 10:38 View Submission | Gradescope
if (tree->right != nullptr)
{
arr[level * 2 + 2] = tree->right->item;
}
Q5
9 Points
True/False
Q5.1
1 Point
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 4/15
8.12.2021 10:38 View Submission | Gradescope
True
False
Q5.2
1 Point
True
False
Q5.3
1 Point
Q5.4
1 Point
True
False
Q5.5
1 Point
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 5/15
8.12.2021 10:38 View Submission | Gradescope
True
False
Q5.6
1 Point
Q5.7
1 Point
Q5.8
1 Point
Q5.9
1 Point
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 6/15
8.12.2021 10:38 View Submission | Gradescope
True
False
Q6
16 Points
Imagine we begin with a sorted list given in the first table below.
Representation format:
letter = D E F A B G I K _ _
link = 1 2 5 4 0 6 7 -1 9 -1
Q6.1
3 Points
After we insert “C” in the correct place for sorted order, what will
be the contents of the list?
Please write the contents (letter and link) of the table as shown
above representation format and write down the values of free_
and *list. (Give your answers in 6.1, 6.2, 6.3, and 6.4)
letter =
DEFABGIKC_
Q6.2
3 Points
link =
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 7/15
8.12.2021 10:38 View Submission | Gradescope
1 2 5 4 8 6 7 -1 0 -1
Q6.3
1 Point
free =
Q6.4
1 Point
*list =
Q6.5
3 Points
After all above operations, we delete “A” and keep the sorted
order, what will be the contents of the list? Please write the
contents (letter and link) of the table in the above representation
format and write down the values of free_ and *list.(Give your
answers in 6.5, 6.6, 6.7 and 6.8)
letter =
DEF_BGIKC_
Q6.6
3 Points
link =
1 2 5 -1 8 6 7 -1 0 -1
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 8/15
8.12.2021 10:38 View Submission | Gradescope
Q6.7
1 Point
free =
Q6.8
1 Point
*list =
Q7
6 Points
Complexity
Q7.1
3 Points
~(logn)
~(n log n)
~(n2 )
~(n2 /2)
~(2n2 )
Q7.2
3 Points
~(logn)
~(2n log n)
~(n log n)
~(n2 /2)
~(n2 )
Q8
5 Points
// makenode: creates a node with the given integer data and returns the
tree = makenode(8);
// insertBST: inserts into BST considering the BST property.
insertBST(tree, 10);
insertBST(tree, 4);
insertBST(tree, 9);
insertBST(tree, 6);
insertBST(tree, 2);
insertBST(tree, 15);
insertBST(tree, 3);
insertBST(tree, 14);
insertBST(tree, 5);
Q8.1
1 Point
Write the data value of the left child of the node 10.
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 10/15
8.12.2021 10:38 View Submission | Gradescope
Q8.2
1 Point
Q8.3
1 Point
True
False
Q8.4
1 Point
Q8.5
1 Point
Write the data value of the first ancestor of the node 15.
10
Q9
6 Points
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 11/15
8.12.2021 10:38 View Submission | Gradescope
section of node p, next(p): the address of the next list item of node
p. Assume that queue has rear and front members.)
insert(queue, x)
{
Node n = getnode();
n->data = x;
n->next = nullptr;
rear->next = n;
rear = n;
}
Q10
20 Points
Also assume that all the operators are associated from left to
right: For instance, A & B & C is processed as ((A & B) & C).
Supplementary Information:
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 12/15
8.12.2021 10:38 View Submission | Gradescope
You can use the following helper functions in your pseudocode (if
needed):
Eval(operator, operand1, operand2) : returns the true value of the
given expression in the form of operand1 operator operand 2, e.g.
Eval(&, B, C) is equivalent to the value of the expression B & C.
Precedence(operator) : returns a constant integer considering the
precedence of operators, e.g. Precedence(&) > Precedence(?) is
True.
Next-token(expr_string) : returns the current token in the input
expression string, i.e. an operand or an operator. The position of
the current token is internally kept track of in this function;
e.g. for the expr_string “B ? C”; Next-token(expr-string) returns B
first; then, Next-token(expr-string) returns ?, and so on. When there
is no more tokens left, it returns null.)
Also, assume that all stack functions are available for use.
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 13/15
8.12.2021 10:38 View Submission | Gradescope
STUDENT
YASİN ŞİMŞEK
TOTAL POINTS
45 / 100 pts
QUESTION 1
QUESTION 2
QUESTION 3
QUESTION 4
QUESTION 5
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 14/15
8.12.2021 10:38 View Submission | Gradescope
QUESTION 6
QUESTION 7
QUESTION 8
QUESTION 9
QUESTION 10
https://www.gradescope.com/courses/326225/assignments/1651465/submissions/100748300 15/15
BBM201-MIDTERM EXAM 1
17 November 2021
SOLUTIONS
Q3. Propose a data structure that supports the stack push and pop operations and a third operation
findMin, which returns the smallest element in the data structure, all in O(1) worst-case time.
Solution:
Let E be our extended stack. We will implement E with two stacks. One stack, which we’ll call
S, is used to keep track of the push and pop operations, and the otherM, keeps track of the minimum.
To implement E.push (x), we perform S.push (x). If x is smaller than or equal to the top element in
stack M, then we also perform M.push (x). To implement E.pop( ) we perform S.pop( ). If x is equal
to the top element in stack M, then we also M.pop( ). E.findMin( ) is performed by examining the
top of M. All these operations are clearly O(1).
Q4. Write a C++ function to return the array of integer data elements of a pointer based binary tree in
level-order. Firstly the root, then nodes at level 2, followed by nodes at level 3, and so on. Your solution
should work in linear time.
Solution:
vector<int> vect;
Queue<Node*> q;
q.enqueue(root);
while (!q.isEmpty()) {
Node* node = q.dequeue();
vect.push_back(node);
if (node->left)
q.enqueue(node->left);
if (node->right)
q.enqueue(node->right);
}
return result;
}
Q9-Assume that we represent a queue as a linked list. Write the pseudocode of the insert operation
(insert(queue, x)) using getnode(), info(p), next(p) pseudo functions that we used to represent linked lists.
(Remember; getnode(): creates an empty node, info(p): the data section of node p, next(p): the address
of the next list item of node p. Assume that the queue has rear and front members.)
Solution:
insert(queue, x)
{
p = getnode();
info(p) = x;
next(p) = null;
if(queue.rear == null)
queue.front = p;
else
next(queue.rear) = p;
queue.rear = p;
}
Q10-Suppose we have three binary operators, namely {&, ?, !} that work with integer operands. Let us
represent the integer operands,which are represented with capital letters for simplicity here (e.g. A, B, C,
.. so on). Assume that the precedence of these operators are defined as follows: & > ? > !
Also assume that all three operators are associated from left to right: For instance, A & B & C is
processed as ((A & B) & C).
You are expected to provide a pseudocode of an expression evaluation algorithm using stacks;
considering the given precedence order and the association properties of the operators. The
algorithm is expected to process an expression string, which contains a sequence of valid symbols from a
given expression, to return the correct value of the expression. (Hint: Use two stacks in your algorithm;
one for the operators and one for the operands, to encode the precedence of the operators properly.)
In your answer; first start by a brief explanation of your algorithm, then provide the pseudocode of
your solution.
Supplementary Information:
You can use the following helper functions in your pseudocode (if needed):
Eval(operator, operand1, operand2): returns the true value of the given expression in the form of
operand1 operator, operand 2, e.g. Eval(&, B, C) is equivalent to the value of the expression B & C.
Precedence(operator): returns a constant integer considering the precedence of operators, e.g.
Precedence(&) > Precedence(?) is True.
Next-token(expr_string): returns the current token in the input expression string, i.e. an operand or an
operator. The position of the current token is internally kept track of in this function;
e.g. for the expr_string “B ? C”; Next-token(expr-string) returns B; then, Next-token(expr-string) returns ?,
and so on. When there are no more tokens left, it returns null.)
Also, assume that all stack functions are available for use.
(Write your assumptions regarding the details of the functions provided above (if needed) and define all
other helper functions that you use in your pseudocode (if needed).)
Solution:
// Assuming we have two stacks;
// operandS : operand stack
// operatorS : operator stack
/* Alg: While iterating over the tokens of the expr-string, each operand is pushed into operandS, each
operator is pushed in operatorS if the current operator has higher precedence than the one on the stack
top. Otherwise (i.e. less than or equal to the stack top), first evaluate the expression for the operator at
the stack top, then push the current operator into the stack. (Expr. evaluations require popping two
operands from the operandS and popping the operator at the stack top and pushing the expr value to the
operandS) */
ExpressionEvaluationAlg(expr-string)
{
while(next=Next-token(expr-string)){
if(is_operand(next))
operandS.push(next)
else{
if (empty(operatorS))
operatorS.push(next);
else{
top_opt = top(operatorS);
if (Precedence(next)>Precedence(top_opt))
operatorS.push(next);
else{
// evaluate the expression
opt = operatorS.pop();
op2 = operandS.pop();
op1 = operandS.pop();
res = Eval(opt, op1, op2);
operarandS.push(res);
operatorS.push(next);
}// else
}// else
}// else
}// while
// empty the operator stack (in case there is any remaining operators inside)
while(!operatorS.empty()){
opt = operatorS.pop();
op2 = operandS.pop();
op1 = operandS.pop();
res = Eval(opt, op1, op2);
operarandS.push(res);
}
val = operandS.pop();
return val;
}
Easy
Medium
1. Trace the given recursive call as we did in class and find what RecursiveFunc do
in general?
int RecursiveFunc (int array[], int index, int n)
{
int val1, val2;
if ( n==1 )
return array[index];
val1 = RecursiveFunc (array, index, n/2);
val2 = RecursiveFunc (array, index+(n/2), n-(n/2));
if (val1 > val2)
return val1;
else
return val2;
}
.. . .
int a[7] = {1,2,10,15,16,4,8};
printf( “ value is %d\n”, RecursiveFunc(a,0,7));
2-Sum
3-Sum
Iterative Fibonacci
Binary Search
6. Write the output of below code segment? Assume the address of z[0][0] is 100.
2 0 3 0
4 0 3 0
5 0 -3 0
20 0 12 0
10 . Assume a new type of special matrix, named stripe matrix, which has non-zero
values in every ath column. Below is a NxN stripe matrix M(N, a) where a is 3.
For this matrix, we would like to store the values, and their coordinates of
the values to save space on the main memory.
a) Give the space usage of such representation as a function of N and a.
b) Explain under what circumstances such representation will use less space
than traditional double array storage.
0 0 2 0 0 3 0
0 0 4 0 0 3 0
0 0 5 0 0 -3 0
0 0 20 0 0 12 0
Solution : a > 2 will save space , since we save 3 values to store 1 value, so at most
we should have n/3 items in the array to save space.
Hard
2. The problem with the recursive Fibonacci number solution was it keeps
calculating the previously found values of lower Fibonacci sequences. For example,
below diagram shows calculating Fib(5) where unnecessarily several same fibonacci
values were calculated repeatedly .
For a solution, assume having an array of memory items, where we keep already
calculated fib (n) values, then we won’t be calculating the same fib(n) values during
the intermediate calculation. Thus using this idea, implement a new recursive
function that keeps a memory of already calculated fib(n) values.
Solution:
memo = {0:0, 1:1}
def fibm(n):
if not n in memo:
memo[n] = fibm(n-1) + fibm(n-2)
return memo[n]
Solution:
def pascal(n):
if n == 1:
return [1]
else:
line = [1]
previous_line = pascal(n-1)
for i in range(len(previous_line)-1):
line.append(previous_line[i] + previous_line[i+1])
line += [1]
return line
4. Write the output of below code segment? Assume the address of z[0][0][0] is 100.
Give a matrix
int z[4][2][6] ;
printf(" *(z[1]+1) = %d\n", * (z[1]+1));
printf(" *(z[1][1]+1) = %d\n", * (z[1][1]+1));
5.In the class we have seen the upper / lower triangular matrices. We calculated the
number of items in lower triangular matrix which was n*(n+1)/2. For a given
position u[i][j] we also showed that the address of u[i][j] is at i(i+1)/2 + j.
Do the same calculations for upper triangular matrix and give the number of items
and the position of u[i][j].
Solution : number of items do not change
The position is 0 row has n items, 1st row has n-1 items, … i th row has n-i
items. We need to add all items including ith row.
n + n -1 + n-2 + … + n-i = n*(n+1)/2 – i*(i+1)/2
= (n^2 + n – i^2 –i )/2
this is number of items before and including the ith row, we need to substract the
items after jth position. There are n-1-j more elements on that row. So we need to
substract n-1 –j and need to increment 1 for starting 0 index
= (n^2 + n – i^2 –i )/2 – (n-1-j) +1
Sample:
0 1 2 3
4 5 6
7 8
i =2 j=2 n=4
16+4-4-2 / 2 – (4-1-2)
14/2 = 7 -1 =6
1. Given an unsorted linked list, and without using a temporary buffer, fill in the
blanks in the below method that will delete any duplicates from the linked list.
(Brute force solution is acceptable)
Answer:
void RemoveDuplicates (Node * head)
{
Node * temp1, * temp2, * prev;
temp1=temp2=prev=head;
while( temp1!=NULL)
{
while( temp2!=NULL)
{
if(temp1->data == temp2->data)
{
//delete
prev ->next = temp2->next;
free(temp2);
}
prev = temp2;
temp2=temp2->next;
}
temp1=temp1->next;
}
}
2. Given two sorted linked lists, merge the lists to form a single sorted linked list.
Reuse one of the lists, Complete the given code below.
Node * newhead,temp;
if (list1->data < list2->data) {
newhead = list1;
list1= list1->next;
}
else {
newhead = list2;
list2 = list2->next;
}
temp = newhead;
while(___________________) {
if (_________ > _________){
temp->next = list2;
list2 = list2->next;
}
else {
temp->next = list1;
list1 = list1->next;
}
temp = temp->next;
}
if (list1 == null)
___________________;
else
___________________;
return newhead;
}
Answer:
Node * newhead,temp;
if (list1->data < list2->data) {
newhead = list1;
list1= list1->next;
}
else {
newhead = list2;
list2 = list2->next;
}
temp = newhead;
return newhead;
}
3. Consider the sorted linked list with these nodes:
NULL
10 40 60 80
ptr1
ptr2 50
The only pointer that is given for this list is ptr1 that is pointing to the node with the
value 60 (shown above) and there is no head pointer provided. There is also a new
node with the value 50 and a pointer, ptr2, pointing to it.
Correctly insert the new node to the above list in the sorted order. Explain your
answer in sentences. No coding is required.
4. Postfix is preferable than infix for computers because of having no checks for
parenthesis and no check for operator precedence rules. Prefix has also the same
properties as postfix for having no parenthesis and precedence check. Can you
explain why postfix is more preferable than prefix?
5. Multiple stacks/queues have a recovery mode if it is full and needs to add more
items. The main idea is finding another stack/queue that has a space, which we can
use by shifting the items. A sample multi queue is given below where there are 5
queues and in each of them there are 4 positions. Queue 0 and Queue 2 are full and
others are not. For each queue a front and a rear pointer is kept.
Explain the steps that need to be taken when adding to a full queue and take
recovery steps. Use short sentences. No coding is required.
Explain the steps that need to be taken when adding to a full queue and take
recovery steps. Write only steps needed to update for the full query and its items
inside. Use short sentences. No coding is required. (Hint there are 2 scenarios)
Veya
Step 1. Check the queues in the right side to find an empty space
Step 1.1 If found move the queues
Step 1.2 move data inside the full queue
Step 1.2.1. __________________________________ (fill here)
Step 2 If no empty queue on the right, check the queues in the left side to find an
empty space
Step 2.1 If found move the queues
Step 2.2 move data inside the full queue
Step 2.2.1. __________________________________ (fill here)