CIS 3490 W25 – Final Exam (April 12, 2:30 PM) Solutions
Instructor: Joe Sawada and Daniel Gabrić
First Name:
Last Name:
Student Number:
The exam must be completed using the provided bubble-sheet.
Select the best answer.
For fairness, we will not answer any questions about the content of the exam while in
session
This exam is closed book and lasts 120 minutes.
You may not use any electronic/mechanical computation devices.
There are 15 pages including the cover page.
1
,1. Consider the following pseudocode given a two dimensional array G, and a list of integers
stored in array A.
1: for i from 1 to n do
2: d←0
3: for j from i + 1 to n do d ← d + G[i][j]
4: Print(“The degree of i is d ”)
5: MergeSort(A[i..n])
Let f (n) denote the number of steps carried out by this code in the worst case, as a function
of the input n. What is the best upper bound for f (n)?
(a) O(n log n)
(b) O(n2 )
(c) O(n2 log n)
(d) O(n3 )
(e) (none of the above)
Solution: (c)
2. Consider the following recursive algorithm where the input is an integer n and a set S =
{s1 , s2 , . . . , sn } with n integers.
1: procedure Recursive(n, S)
2: if n = 1 then return
3: Recursive(n − 2, {s3 , s4 , . . . , sn })
4: for i from 1 to n do Output si
5: if sn−1 > sn then
6: Swap sn−1 and sn
7: Recursive(n − 1, {s1 , s2 , . . . , sn−1 })
8: else
9: Recursive(n − 1, {s1 , s2 , . . . , sn−1 })
Let T (n) denote the running time of the above algorithm where T (1) = Θ(1). What is T (n)
for n > 1?
(a) 2T (n − 1) + T (n − 2)
(b) 2T (n − 1) + T (n − 2) + O(n2 )
(c) T (n − 1) + T (n − 2) + O(n)
(d) T (n − 1) + T (n − 2) + O(1)
2
, (e) (none of the above)
Solution: (c)
3. Consider a problem P. Suppose there exists an algorithm to solve P that runs in O(n2 ) time
for every possible input. Then a lower bound for the problem P is
(a) Ω(n2 )
(b) Θ(n2 )
(c) Θ(1)
(d) There is not enough information to determine a lower bound
(e) (none of the above)
Solution: (d)
4. Consider an implementation of QuickSort that uses a randomized pivot. What is an upper
bound on the worst case and expected case of this algorithm?
(a) O(n2 ) and O(n log n)
(b) O(n2 ) and O(n2 )
(c) O(n log n) and O(n)
(d) O(n log n) and O(n log n)
(e) (none of the above)
Solution: (a)
5. Suppose you have a set of n integers in the range 1 to 143, 000 (roughly the population of
Guelph). Suppose the values are not uniformly distributed. What is the best upper bound
on the time required to sort these integers?
(a) O(1)
(b) O(log n)
(c) O(n)
(d) O(n log n)
(e) (none of the above)
Solution: (c) Use a bucket sort, since there is a constant number of options.
6. What is the worst-case running time of randomized quick select and deterministic select,
respectively, to find the k-th smallest value in a list of n numbers?
(a) O(log n) and O(n)
3