DAA Study Hub

Mock midterm B (exam mode)

The second timed paper, with traps that catch common beginner mistakes.

How to use exam mode

Same rules as Paper A: closed book, a 75-minute phone timer, and no answers opened until it rings. Take it after Paper A and after reviewing your mistakes, so it measures what you learned. Several questions have a twist that catches a common beginner trap. Read each one carefully.

Total: 50 marks


Q1. Warm-up (6 marks)

(a) Simplify log⁡3(81n2)\log_3(81n^2). [2]

(b) Give a closed form for ∑i=1n(2i−1)\sum_{i=1}^{n}(2i - 1). [2]

(c) Evaluate ∑i=062i\sum_{i=0}^{6} 2^i with the geometric-series formula. [2]

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 n=16n = 16, exactly how many times does x ← x + 1 run? [3]

(b) Give the Θ bound for general nn and justify it. (Careful: it is not nlog⁡nn\log n.) [3]

Q3. Asymptotic proofs (10 marks)

(a) Prove 2n3−5n2+1=Θ(n3)2n^3 - 5n^2 + 1 = \Theta(n^3) with explicit c1,c2,n0c_1, c_2, n_0. [5]

(b) True or false? Give a one-line reason for each. [5]

(i) n2+100n=O(n2)n^2 + 100n = O(n^2)
(ii) 3n=O(2n)3^n = O(2^n)
(iii) nlog⁡n=Ω(n)\frac{n}{\log n} = \Omega(\sqrt n)
(iv) 2log⁡2n=Θ(n)2^{\log_2 n} = \Theta(n)
(v) n0.01=O(log⁡2n)n^{0.01} = O(\log^2 n)

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 nn that makes insertion sort do the most shifts, and give that number exactly. What input gives the fewest? [4]

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 m

State a loop invariant and prove initialization, maintenance and termination.

Q6. Induction (6 marks)

Prove that 33 divides n3+2nn^3 + 2n for every integer n≥1n \ge 1.

Q7. Recurrences (8 marks)

(a) Solve T(n)=T(n−1)+nT(n) = T(n-1) + n, T(1)=1T(1) = 1, by expansion (iteration). Give the exact closed form and the Θ. [3]

(b) Solve T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1, T(1)=1T(1) = 1, with a recursion tree: give the level table and the Θ. [3]

(c) Use substitution to show T(n)=T(n/2)+nT(n) = T(n/2) + n, T(1)=1T(1) = 1, satisfies T(n)≤2nT(n) \le 2n. [2]


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).

B

On this page