Mock midterm B (exam mode)
The second timed paper, with traps that catch common beginner mistakes.
How to use exam mode
Total: 50 marks
Q1. Warm-up (6 marks)
(a) Simplify . [2]
(b) Give a closed form for . [2]
(c) Evaluate with the geometric-series formula. [2]
(a) .
(b) (the sum of the first odd numbers is a perfect square).
(c) .
Q2. Counting steps (6 marks)
i ← n
while i > 1 do
for j ← 1 to i do
x ← x + 1
i ← ⌊i / 2⌋(a) For , exactly how many times does x ← x + 1 run? [3]
(b) Give the Θ bound for general and justify it. (Careful: it is not .) [3]
(a) takes the values 16, 8, 4, 2, so .
(b) The inner loop does steps. That is a geometric series bounded by , and the first pass alone is , so the total is . The outer loop runs times, but the passes shrink, so you can't multiply .
Q3. Asymptotic proofs (10 marks)
(a) Prove with explicit . [5]
(b) True or false? Give a one-line reason for each. [5]
(i)
(ii)
(iii)
(iv)
(v)
(a) Upper: for , and , so : . Lower: for , , so : . . Check : ✓ (at the lower bound fails: ).
(b) (i) T: , so . (ii) F: . (iii) T: .
(iv) T: exactly. (v) F: every positive power of eventually beats any power of .
Q4. Insertion sort (8 marks)
(a) Trace insertion sort on [4, 9, 2, 7, 1]. Show the array after each outer iteration, and count both shifts and key comparisons. [4]
(b) Describe the input of size that makes insertion sort do the most shifts, and give that number exactly. What input gives the fewest? [4]
(a) : [4, 9, 2, 7, 1] (0 shifts, 1 comparison) · : [2, 4, 9, 7, 1] (2, 2) · : [2, 4, 7, 9, 1] (1, 2) · : [1, 2, 4, 7, 9] (4, 4).
7 shifts, 9 comparisons. (When the key runs off the left end, the loop stops on without a comparison.)
(b) A reverse-sorted array: key shifts elements, so , which is . An already-sorted array gives 0 shifts and comparisons, which is .
Q5. Correctness (6 marks)
MaxIndex(A, n)
1 m ← 1
2 for i ← 2 to n do
3 if A[i] > A[m] then m ← i
4 return mState a loop invariant and prove initialization, maintenance and termination.
Invariant: at the start of the iteration with index , is a largest value in . Init: , the range is , and ✓. Maintenance: if , the new element is bigger than everything before, so makes the max of . Otherwise and stays the max ✓. Termination: the loop exits with , so is a largest value in and the function returns its index ∎
Q6. Induction (6 marks)
Prove that divides for every integer .
Base : ✓. Step: assume . Then . The first bracket is divisible by 3 (the hypothesis) and the second is 3 times an integer, so the sum is divisible by 3 ∎
Q7. Recurrences (8 marks)
(a) Solve , , by expansion (iteration). Give the exact closed form and the Θ. [3]
(b) Solve , , with a recursion tree: give the level table and the Θ. [3]
(c) Use substitution to show , , satisfies . [2]
(a) , which is .
(b) Level has nodes, each costing 1, so the level costs . Levels , so the total is , which is . (A trap: the work is at the leaves, not .)
(c) Base: ✓. Step: assume . Then ✓ ∎
Score bands: 45+ ready · 35–44 review your mistakes · below 35 redo the weakest topics in a different study mode.
Log my score
Marked yourself? Save the score to My progress (stays in this browser).