ICS202 202 Midterm Solved
ICS202 202 Midterm Solved
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.
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”
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.
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
Page 3 of 13
if(head == null || head.next == null || head.next.next == null)
return;
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.
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.
s1.push(s.charAt(ix) + “”);
while(!s1.isEmpty())
s2 = s2 + s1.pop();
return s1.equals(s2);
10 * 8 + (8 / 4 – 3)
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 *]
|2|
Push 2, Push 3, Remaining Expression: *
|3|
Page 5 of 13
Stack Contents Operations
|4 |
|8 | Push 8, Push 4, Remaining Expression: /
| 80 |
|3 |
|2 | Push 3, Remaining Expression: –
| 80 |
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).
n i3 n n(n + 2 n + 1)
1 = i3 = [ n ( n + 1) / 2 ]2 = 4
i=1 j=1 i=1
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:
(a)
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.
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
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.
(d) Write the recursive method that is called by the method you wrote in (c)
Page 10 of 13
Q. 5: (a) Given the following BSTNode class:
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:
Page 12 of 13
Quick Reference Sheet
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