UNIVERSITY OF TORONTO
Faculty of Arts and Science
Midterm 2, Version 3
CSC165H1S
Date: Friday March 24, 2:10-3:00pm
Duration: 50 minutes
Instructor(s): David Liu, Toniann Pitassi
No Aids Allowed
Name:
Student Number:
Please read the following guidelines carefully!
• Please write your name on both the front and back of this exam.
• This examination has 4 questions. There are a total of 8 pages, DOUBLE-SIDED.
• Answer questions clearly and completely. Provide justification unless explicitly asked not to.
• Formal proofs should follow the same guidelines from the first half of the course (e.g., explicitly
introduce all variables and assumptions, clearly state all assumptions and reasoning you make in
your proof body, etc.)
• For algorithm analysis questions (including worst-case and best-case), you may freely use external
properties of Big-Oh/Omega/Theta presented in the course. You can jump immediately from a step
count to an asymptotic bound without proof (e.g., say “the number of steps is 3n + log n, which is
Θ(n)”).
• For all other questions, you may not use these properties, or other external facts about definitions
introduced in this course, unless explictly allowed to.
Take a deep breath.
This is your chance to show us
How much you’ve learned.
We WANT to give you the credit
That you’ve earned.
A number does not define you.
Good luck!
CSC165H1S , Winter 2017
Midterm 2, Version 3
Use this page for rough work. If you want work on this page to be marked, please indicate this clearly at
the location of the original question.
Page 2/8
CSC165H1S , Winter 2017
Midterm 2, Version 3
1. [5 marks] Induction. Prove the following statement using induction on n:
n
3
n
.
∀n ∈ N, n ≥ 2 ⇒ + 1 ≤
2
2
n+1 n
n
3
3
1
3
Hint:
=
+ ·
2
2
2
2
Solution
Proof. Let P (n) be the predicate “
n
+1≤
2
n
3
.” We prove that for all n ∈ N, n ≥ 2 ⇒ P (n).
2
Base case: let n = 2.
2
2
3
9
In this case, the left side of the inequality is + 1 = 2. The right side is
= , and so the two
2
2
4
sides are equal.
n
n
3
. We want to prove that
Induction step: let n ∈ N, and assume that n ≥ 2 and that + 1 ≤
2
2
n+1
n+1
3
+1≤
.
2
2
By the induction hypothesis, we know that:
n
n
3
+1≤
2
2
n
3
1
n 1
+ +1≤
+
2 2
2
2
n
n+1
3
1
+1≤
+
2
2
2
n
3
1 3 n
≤
+
2
2 2
n+1
3
=
(the hint)
2
Page 3/8
CSC165H1S , Winter 2017
Midterm 2, Version 3
2. [6 marks] Worst-case runtime. Consider the following algorithm, which takes as input a list of integers.
def alg(A):
n = len(A)
count = 0
for i in range(n):
if count >= n:
print('Reached limit')
break
else:
count = count + A[i]
# Loop 1
for j in range(min(count, n)):
for k in range(j):
print('Hello')
# Loop 2
# Loop 3
Let W C(n) be the worst-case runtime function of alg, where n is the length of the input list A. You can
use the following formula in your analysis of W C(n):
∀m ∈ N,
m
X
i=1
i=
m(m + 1)
2
Note: assume the integers stored in A can be arbitrarily large (i.e., don’t assume some upper limit on
the numbers in A).
(a) [4 marks] Find, with proof, a good asymptotic upper bound (Big-Oh) on W C(n). By “good” we
mean that if you prove W C ∈ O(f ) (where you chose the f ), it should be true that W C ∈ Ω(f ) as
well (but don’t prove this here).
Solution
Let n ∈ N, and consider the running time of alg on an input of length n. Loop 1 has at most
n iterations, with each iteration taking a single step (constant time block of code). So the cost
of Loop 1 is at most n steps.
For a fixed iteration of Loop 2, the inner loop (Loop 3) takes j steps, since it has j iterations
and each iteration takes 1 step. Since min(count, n) ≤ n, Loop 2 will iterate at most n times,
n−1
X
n(n − 1)
for j = 0, 1, . . . , n − 1. Therefore the total cost of the outer loop is at most
i=
2
i=0
steps.
So the total runtime is at most n + n(n − 1)/2, which is O(n2 ).
(b) [2 marks] Describe an input family whose runtime matches the upper bound you proved in part
(a). For example, if you proved that W C(n) ∈ O(n), for this part you should describe an input
family whose runtime is Θ(n).
Only a description of the input family is necessary; you do not need to analyse the running time of
alg on your chosen input family.
Page 4/8
CSC165H1S , Winter 2017
Midterm 2, Version 3
Solution
For each n ∈ N, consider the list A where A[0] = 0 and A[1] = A[2] = · · · = A[n − 1] = 1.
3. [4 marks] Best-case runtime. Let BC(n) be the best-case running time of the algorithm alg from
Question 2. Prove that BC(n) ∈ O(n), where n represents the length of the input list. You may assume
that n > 0 for this analysis.
Solution
We need to find an input family for alg whose running time is O(n). For each n ∈ N, choose the
input list of length n whose elements are all equal to 0.
Consider what happens when we run alg on the input list of length n. First, Loop 1 will run n times,
with each iteration taking a single step. The cost here is n. The value of variable count is equal to
0 throughout the entire run of Loop 1, and after it ends, since the number 0 is repeatedly added to
count.
So then Loops 2 and 3 don’t run any times (since min(n, count) = 0), and so the total cost is n,
which is O(n).
Page 5/8
CSC165H1S , Winter 2017
Midterm 2, Version 3
4. [5 marks] Properties of Big-Oh. For all functions f, g ∈ N → R≥0 , we define their product function,
denoted f × g, to be the following function:
(f × g)(n) = f (n) × g(n)
for all n ∈ N.
Prove that for all functions f1 , f2 , g1 , g2 : N → R≥0 , if g1 ∈ O(f1 ) and g2 ∈ O(f2 ), then g1 ×g2 ∈ O(f1 ×f2 ).
Reminder: you may not use any properties of Big-Oh in this question. You should use the definition of
Big-Oh:
g ∈ O(f ) :
∃c, n0 ∈ R+ , ∀n ∈ N, n ≥ n0 ⇒ g(n) ≤ cf (n),
where f, g : N → R≥0
Solution
Proof. Let f1 , f2 , g1 , g2 : N → R≥0 . Assume that g1 ∈ O(f1 ) and g2 ∈ O(f2 ), i.e., assume there exist
c1 , n1 , c2 , n2 ∈ R+ such that for all n ∈ N:
(i) if n ≥ n1 , then g1 (n) ≤ c1 f1 (n)
(ii) if n ≥ n2 , then g2 (n) ≤ c2 f2 (n)
We want to prove that g1 × g2 ∈ O(f1 × f2 ). Let c3 = c1 c2 and n3 = n1 + n2 .∗ Let n ∈ N, and assume
that n ≥ n3 . We want to prove that g1 (n) × g2 (n) ≤ c3 f1 (n) × f2 (n).
Since n ≥ n3 = n1 + n2 , we know that n ≥ n1 and n ≥ n2 . So then by assumption (i), we know that
g1 (n) ≤ c1 f1 (n), and by assumption (ii) we know that g2 (n) ≤ c2 f2 (n).
Multiplying these inequalities gives us the inequality
g1 (n) × g2 (n) ≤ (c1 f1 (n)) × (c2 f2 (n))
= (c1 c2 )f1 (n) × f2 (n)
= c3 f1 (n) × f2 (n)
∗
n3 = max(n1 , n2 ) is also acceptable.
Page 6/8
CSC165H1S , Winter 2017
Midterm 2, Version 3
Use this page for rough work. If you want work on this page to be marked, please indicate this clearly at
the location of the original question.
Page 7/8
CSC165H1S , Winter 2017
Midterm 2, Version 3
Name:
Question Grade Out of
Q1
5
Q2
6
Q3
4
Q4
5
Total
20
Page 8/8
© Copyright 2026 Paperzz