Mock midterm A (exam mode)
75 minutes, 50 marks, closed book. Answers stay folded until the timer rings.
How to use exam mode
- Get paper and a pen, then set a 75-minute timer on your phone. Closed book: no notes, no cheatsheet, no other tabs.
- Every answer below is folded shut. Don't open any until the timer rings.
- Mark yourself with the answers. Copy every mistake into My mistakes and write the score with the Log my score form at the bottom of this page.
- Two days later, redo only the questions you missed. Then try Paper B.
The questions mix all topics, the way an exam might. They are new problems written for practice, not copies of slide or book exercises.
Total: 50 marks
Q1. Warm-up (6 marks)
(a) Simplify . [2]
(b) Give a closed form for . [2]
(c) Evaluate using the geometric-series formula. [2]
(a) .
(b) .
(c) .
Q2. Counting steps (6 marks)
for i ← 1 to n do
for j ← i to n do
for k ← 1 to 10 do
x ← x + 1(a) Exactly how many times does x ← x + 1 run? [3]
(b) Give the Θ bound and explain the role of the k loop. [3]
(a) The j loop runs times, so the total is .
(b) . The k loop is a constant (10) factor, so it doesn't change the growth rate.
Q3. Asymptotic proofs (10 marks)
(a) Prove with explicit . [5]
(b) Rank from slowest- to fastest-growing: , , , , . [5]
(a) Upper: for , , so . Lower: when , i.e. , so . Take . Check : ✓
(b) .
Q4. Insertion sort (8 marks)
(a) Trace insertion sort on [6, 2, 8, 1, 4]. Show the array after each outer iteration and count the shifts. [4]
(b) State the loop invariant and explain the maintenance step in your own words. [4]
(a) : [2, 6, 8, 1, 4] (1) · : [2, 6, 8, 1, 4] (0) · : [1, 2, 6, 8, 4] (3) · : [1, 2, 4, 6, 8] (2). 6 shifts.
(b) At the start of each iteration, A[1..j-1] holds the original first elements in sorted order. Maintenance: elements bigger than the key shift right by one, keeping their order, and the key drops into the gap after the last element ≤ key, so A[1..j] is sorted.
Q5. Correctness (6 marks)
CountEven(A, n)
1 c ← 0
2 for i ← 1 to n do
3 if A[i] mod 2 = 0 then c ← c + 1
4 return cState a loop invariant and prove initialization, maintenance and termination.
Invariant: at the start of iteration , = the number of even elements in . Init: , the range is empty, and ✓. Maintenance: if is even, goes up by 1, otherwise it is unchanged, so counts the evens in ✓. Termination: the loop exits at , so = the number of evens in ∎
Q6. Induction (6 marks)
Prove for all : .
Base : LHS , RHS ✓. Step: assume it holds for . Then , which is the formula with ✓ ∎
Q7. Merge sort & recurrences (8 marks)
(a) Show the merge of [3, 5, 9] and [2, 4, 10, 11]. How many comparisons? [2]
(b) Solve with a recursion tree: give the level table and the Θ answer. [4]
(c) Solve . [2]
(a) 2, 3, 4, 5, 9, 10, 11. Comparisons: 3v2, 3v4, 5v4, 5v10, 9v10, then the left list is empty: 5.
(b) Level : nodes of size , total , which grows. The leaves dominate: , so .
(c) One node per level, levels, so .
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).