0% found this document useful (0 votes)
11 views13 pages

ICS202 202 Midterm Solved

Uploaded by

jt89xgmzxd
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)
11 views13 pages

ICS202 202 Midterm Solved

Uploaded by

jt89xgmzxd
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/ 13

King Fahd University of Petroleum & Minerals

College of Computer Science and Engineering


Information and Computer Science Department
Second Semester 202 (2020/2021)

ICS 202 – Data Structures


Midterm Exam
Tuesday, 16th March 2021
Time: 120 minutes

Name: _______________________________________________

ID#

Section: ____________

1. This exam consists of 13 pages including (1) title page, (2) statement to ensure
non-cheating and (13) reference sheet
2. It is required to sign your name on the (2) statement to ensure non-cheating. You
may either print and sign it or use an MS-Paint Signature (please keep it ready
before the exam).
3. Please solve this exam on this template. Use either (a) a printed version, solve
by hand and scan or (b) solve it on your PC, or (c) use a tablet-stylus
combination.
4. Please upload your solution as a pdf.
5. There will be a small penalty for solving this exam on plain paper. No
excuses in this regard.

Question # Max Points Points Earned Comments

1 [Linked Lists] 20

2 [Stacks, Queues] 20

3 [Complexity] 10

4 [Recursion] 30

5 [Binary Trees] 20

Total 100

Page 1 of 13
Approved “Student Declaration Statement
for Exam Integrity”

We wish you all the best in the exam.

Please read carefully and accept the following before proceeding to the exam.

I declare that:

• I will complete this assessment entirely by myself without taking any assistance
whatsoever from a person, resource or tool other than what is explicitly permitted
within the regulations prescribed for this assessment.
• I will not disclose, share or discuss the material of this assessment in any form
with anyone except what has been authorized.
• I will uphold the highest standards of honesty and integrity for completing this
assessment to the best of my knowledge.
• I am fully accountable for all rules pertaining to this assessment.
• I understand that ignorance of a rule or a principle is not an excuse for any
misconduct.
• I understand that engaging in an act of a misconduct would result in imposition of
penalties such as failing this course and/or a dismissal from the University.

‫مع امنياتنا لكم بالتوفيق والنجاح في هذا االختبار نرجو قراءة النص التالي بعناية والموافقة عليه قبل البدء باالختبار‬
: ‫أقر بـ‬
‫أن أنهي االختبار بأكمله بنفسي وبدون أن أتلقى أي مساعدة من أي شخص أو من أي مصدر بخالف ما هو منصوص عليه‬ •
.‫بوضوح في لوائح هذا االختبار‬
.‫• أن ال أقوم بنشر أو بمشاركة أو بمناقشة أي من محتويات هذا االختبار مع أي شخص باستثناء ما سمح لي فيه‬
.‫• االلتزام بأعلى معايير األمانة والنزاهة في إكمال هذا االختبار‬
.‫• أن أكون مسؤوالا مسؤولية تامة عن تطبيق جميع األنظمة المتعلقة بهذا االختبار‬
.‫• أن جهلي بأي من أنظمة أو متطلبات هذا االختبار ال تعفيني من المسؤولية تجاه أي تصرف غير مقبول‬
‫• علمي بأن أي تصرف من قبلي يخل باالمانة او النزاهة في اخذ االختبارقد يعرضني إلى العقوبات التي تقرها الجامعة‬
.‫أو الفصل من الجامعة‬/‫والتي قد تصل إلى الرسوب في المقرر و‬

Name: _________________________________

Signature: ______________________________

Date: __________________________________

Page 2 of 13
Q. 1: (a) [14 points] Consider a singly linked list represented by the class SLL<T> as shown in the
reference sheet. Design and implement the following two methods:

(i) deleteThird which deletes the third element of a singly linked list.
(ii) deleteThirdLast which deletes the third last of a singly linked list.

The effect of these methods is shown here (initial linked list):

3 4 7 8 1 2 5 null

head tail
[After applying myList.deleteThird()]

3 4 8 1 2 5 null

head tail
[After applying myList.deleteThirdLast()]

3 4 7 8 2 5 null

head tail

Do not use the methods addToHead, addToTail, deleteFromHead, deleteFromTail.


Instead, directly manipulate list nodes/pointers for both the methods.
Make sure to take care of all special cases
(If the third or third last element does not exist, just return from the method/s).

(i) public void deleteThird() {

if(head == null || head.next == null || head.next.next == null)


return;
if(head.next.next == tail) //only three elements

tail = head.next; //tail becomes the second element

head.next.next = head.next.next.next; //takes care of all cases

(ii) public void deleteThirdLast() {

Page 3 of 13
if(head == null || head.next == null || head.next.next == null)

return;

if(head.next.next == tail) // only three elements

{ head = head.next; return; } //delete the first element

SLLNode<T> prev = head;

while(prev.next.next.next != tail) prev = prev.next;

prev.next = prev.next.next;

return;

Q. 1(b) [6 points] What is the big-O time complexity of both your methods in terms of list size n.

Big-O Complexity of deleteThird(): O(1)

since no traversal of list is involved

Big-O Complexity of deleteThirdLast(): O(n)

since list is traversed to find the third last element

Page 4 of 13
Q. 2 (a) [10 points] Write a method public static boolean isPalindrome(String s) that
determines whether an input string s is a palindrome or not. [A palindrome is a string that reads the
same forwards and backwards. For example: level is a palindrome, but lever is not]. Do not use
arrays or any other data structure for this program except Stacks. Consider using multiple stacks.

public static boolean isPalindrome(String s) {

Stack s1 = new Stack(); String s2 = new String();

for(int ix = 0; ix < s.length(); ix++)

s1.push(s.charAt(ix) + “”);

while(!s1.isEmpty())

s2 = s2 + s1.pop();

return s1.equals(s2);

Q. 2 (b) [10 points] Given the following infix expression:

10 * 8 + (8 / 4 – 3)

(i) Give the equivalent postfix expression.

10 8 * 8 4 / 3 - +

(ii) Using a stack, evaluate this postfix expression (Give contents of stack at each stage).
[The first two rows are just examples for the expression 2 3 *]

Stack Contents Operations

|2|
Push 2, Push 3, Remaining Expression: *
|3|

|6| Pop (3), Pop (2), 3*2 = 6, Push(6)

Page 5 of 13
Stack Contents Operations

|8 | Push 10, Push 8, Remaining Expression: *


| 10 |

| 80 | Pop (8), Pop (10), 10 * 8 = 80, Push(80)

|4 |
|8 | Push 8, Push 4, Remaining Expression: /
| 80 |

|2 | Pop (4), Pop (8), 8 / 4 = 2, Push(2)


| 80 |

|3 |
|2 | Push 3, Remaining Expression: –
| 80 |

| –1| Pop (3), Pop (2), 2 – 3 = –1, Push(–1),


| 80 | Remaining Operation +

| 79 | Pop (–1), Pop (80), 80 + –1 = 79, Push(79)

Pop (79) : Final Answer

Page 6 of 13
Q. 3: [7 + 3 = 10 points] .

(a) Given the following method, how many times is MyStatement executed as a function of n.
(b) Give the big-O complexity of this code fragment in terms of O(n).

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


sum[i] = 0;
for (j = 1; j <= i3 ; j++)
sum[i] = sum[i] + j; // MyStatement
}
return true;

n i3 n n(n + 2 n + 1)
  1 =  i3 = [ n ( n + 1) / 2 ]2 = 4
i=1 j=1 i=1

Big-O complexity: O(n2)

Page 7 of 13
Q. 4 [30 points: 2 + 15 + 13 = 30 points]

(a) Write the recurrence relation that represents the number of additions T(n) as a function of n in
the following method:

public static int myMethod(int n){


if(n == 0)
return 0;
else{
System.out.println(n);
return myMethod(n - 2) + n;
}
}

Note: DO NOT EXPAND THE RECURRENCE RELATION

(a)

T(n) = 0 for n = 0, T(n) = T(n – 2) + 1, otherwise for n > 0.

Page 8 of 13
(b) [15 points] The running time T(n) of an algorithm is represented by the following
recurrence relation:

T (0) = a
n n  0
T (n) = T (n − 1) + + b
2
Where a and b are constants. Solve the recurrence relation by iteration and then
determine the big-O complexity of the algorithm.

You may find the following summation formulae useful:

n
n(n + 1) n
n(n + 1)(2n + 1) k −1
1 1 k −1

i =
i =1 2
i
i =1
2
=
6
2
i =0
i
= 2−
2 k −1
2
i =0
i
= 2k − 1

Solution:
n
T (n) = T (n − 1) + +b
2
(n − 1) n
= [T (n − 2) + + b] + + b
2 2
1
= T (n − 2) + ((n − 1) + n) + 2b
2
(n − 2) 1
= [T (n − 3) + + b] + ((n − 1) + n) + 2b
2 2
1
= T (n − 3) + ((n − 2) + (n − 1) + n) + 3b
2
....................................................
1
= T (n − k ) + ((n − k + 1) + ... + (n − 2) + (n − 1) + n) + kb
2
By substituting k = n,

1
T (n) = T (0) + (1 + 2 + ...+ (n − 1) + n) + nb
2
1 n
1 n(n + 1) n(n + 1) n2 n
= a +  i + nb = a + ( ) + nb = a + + nb = a + + + nb
2 i =1 2 2 4 4 4

Therefore, T(n) = O(n2)

Page 9 of 13
(c) Write a method: public static getMax(int[] array) that calls a private static recursive
method getMax which returns the maximum value in the array.

public static int getMax(int[] array){


int currentMax = array[array.length - 1];
return getMax(array, currentMax, array.length - 1);
}

(d) Write the recursive method that is called by the method you wrote in (c)

private static int getMax(int[] array, int currentMax, int index){


if(index == -1)
return currentMax;
else if(array[index] > currentMax)
return getMax(array, array[index], index - 1);
else
return getMax(array, currentMax, index - 1);
}

Page 10 of 13
Q. 5: (a) Given the following BSTNode class:

public class BSTNode<T extends Comparable<? super T>> {


protected T el;
protected BSTNode<T> left, right;
public BSTNode() {
left = right = null;
}
public BSTNode(T el) {
this(el,null,null);
}
public BSTNode(T el,BSTNode<T> lt, BSTNode<T> rt){
this.el = el; left = lt; right = rt;
}
}

And the following instance methods of BinarySearchTree class:

public void myTraversal(){


myTraversal(root);
}

private void myTraversal(BSTNode node){


if(node != null){
System.out.print(node.el + " ");
myTraversal(node.left);
myTraversal(node.right);
System.out.print(node.el + " ");
}
}

What is the output of: tree.myTraversal( ); if tree is the following BinarySearchTree?

8 3 7 7 3 9 9 8

Page 11 of 13
(b) Draw the resulting Binary search tree after deleting 24 by ANY deletion by copying method
from the following BST:

(c) Give the inorder, preorder, and postorder traversals of the following BinaryTree:

Traversal type traversal


Inorder 2, 5, 10, 13, 35, 45, 40, 8

preorder 35, 5, 2, 13, 10, 40, 45, 8

postorder 2, 10, 13, 5, 45, 8, 40, 35

Page 12 of 13
Quick Reference Sheet

public class SLLNode<T> { public class Stack<T> {


public T info; private …; // array or linked list
public SLLNode<T> next; public Stack();
public SLLNode(); public Stack(int n);
public SLLNode(T el) public void clear();
public SLLNode(T el, SLLNode<T> ptr); public boolean isEmpty();
} public T topEl();
public T pop();
public class SLL<T> { public void push(T el);
protected SLLNode<T> head, tail; public String toString();
public SLL(); }
public boolean isEmpty();
public void addToHead(T el); public class Queue<T> {
public void addToTail(T el); private …; // array or linked list
public T deleteFromHead(); public Queue();
public T deleteFromTail(); public void clear();
public void delete(T el); public boolean isEmpty();
public void printAll(); public T firstEl();
public boolean isInList(T el); public T dequeue();
} public void enqueue(T el);
public String toString();
public class DLLNode<T> { }
public T info;
public DLLNode<T> next, prev;
public DLLNode();
public DLLNode(T el);
public DLLNode(T el, DLLNode<T> n,
DLLNode<T> p);
}

public class DLL<T> {


private DLLNode<T> head, tail;
public DLL();
public boolean isEmpty();
public void setToNull();
public void addToHead(T el);
public void addToTail(T el);
public T deleteFromHead();
public T deleteFromTail();
public void delete(T el);
public void printAll();
public boolean isInList(T el);
}

n(n + 1) n(n + 1)(2n + 1)  n(n + 1) 


n n 2

i = i 2 =
n

 i3 =  
i =1 2 , i =1 6 , i =1  2  ,
n +1
n
x −1
x
i =0
i
=
n −1

Page 13 of 13

You might also like