Complexity Analysis
Chapter: 3.1 Complexity Analysis Level: 1 — Recognition (MCQ + Matching + True/False with justification) Time limit: 20 minutes Total marks: 30
Section A — Multiple Choice (1 mark each)
Choose the single best answer.
Q1. By the formal definition, means there exist constants and such that for all :
- (a)
- (b)
- (c)
- (d)
Q2. Which complexity grows the fastest as ?
- (a)
- (b)
- (c)
- (d)
Q3. Binary search on a sorted array of elements has worst-case time complexity:
- (a)
- (b)
- (c)
- (d)
Q4. The Master Theorem for compares against:
- (a)
- (b)
- (c)
- (d)
Q5. Auxiliary space complexity refers to:
- (a) total memory including the input
- (b) extra space used excluding the input
- (c) only the space used by recursion
- (d) space measured in bits, not bytes
Q6. For the recurrence , the solution is:
- (a)
- (b)
- (c)
- (d)
Q7. Amortized analysis is used to:
- (a) find the exact worst case of a single operation
- (b) average the cost over a sequence of operations
- (c) measure only the best-case cost
- (d) compute space rather than time
Q8. provides a:
- (a) tight bound (both upper and lower)
- (b) upper bound only
- (c) lower bound only
- (d) average-case bound only
Q9. Linear search's best case (target is first element) is:
- (a)
- (b)
- (c)
- (d)
Q10. The dynamic-array (doubling) push operation has amortized time per push of:
- (a)
- (b)
- (c)
- (d)
Section B — Matching (1 mark each, 6 marks)
Q11. Match each algorithm/scenario (left) to its typical time complexity (right).
| # | Item | Complexity | |
|---|---|---|---|
| i | Accessing array element by index | A | |
| ii | Merge sort | B | |
| iii | Naïve nested-loop bubble sort | C | |
| iv | Generating all subsets of a set | D | |
| v | Traversing a linked list once | E | |
| vi | Brute-force travelling salesman (all permutations) | F |
Write pairs, e.g. i–B.
Section C — True/False WITH Justification (2 marks each: 1 verdict + 1 justification)
Q12. . (T/F + justify)
Q13. If then and . (T/F + justify)
Q14. is asymptotically worse than . (T/F + justify)
Q15. The recurrence solves to . (T/F + justify)
Q16. In quicksort, the worst case () and average case () differ; therefore Big-O of the worst case is . (T/F + justify)
Q17. In the Master Theorem, if , then . (T/F + justify)
Q18. An algorithm that uses auxiliary space always uses total space. (T/F + justify)
Answer keyMark scheme & solutions
Section A (10 marks)
Q1 — (b) for all . Big-O is an upper bound. (1)
Q2 — (c) . Growth order: . Factorial dominates exponential (Stirling: beats ). (1)
Q3 — (b) — each step halves the search interval. (1)
Q4 — (b) — the "watershed" function of the Master Theorem. (1)
Q5 — (b) Extra space excluding input. Total = input + auxiliary. (1)
Q6 — (c) . Here ; , so Case 2 gives . (1)
Q7 — (b) Averages cost across a sequence of operations (worst-case average). (1)
Q8 — (a) Tight bound: . (1)
Q9 — (c) — found on first comparison. (1)
Q10 — (c) amortized (doubling spreads resize cost). (1)
Section B (6 marks)
Q11.
- i–B ( index access)
- ii–D (merge sort )
- iii–A (bubble sort )
- iv–C (subsets: )
- v–F (linked-list traversal )
- vi–E (all permutations )
1 mark each correct pair.
Section C (14 marks)
Q12 — TRUE. (1) Justification: choose ; then for all . Lower-order term is dominated. (1)
Q13 — TRUE. (1) is defined exactly as the intersection of (upper) and (lower); by definition it implies both. (1)
Q14 — FALSE. (1) grows slower than (since ), so it is better, not worse. (1)
Q15 — FALSE. (1) Unrolling gives , a linear sum, not logarithmic. (1)
Q16 — FALSE. (1) The worst case is ; you cannot bound the worst case by . Best/average/worst are separate cases each with their own bound. (1)
Q17 — TRUE. (1) This is Master Theorem Case 2: when , we get . (1)
Q18 — FALSE. (1) Total space always includes the input, which is for an input of size ; so total space is even when auxiliary is . (1)
[
{"claim":"Q6/Q17: T(n)=2T(n/2)+n gives Theta(n log n) via Master Case 2 since n^{log_2 2}=n",
"code":"a=2; b=2; e=Rational(a).__class__; import sympy; logba=sympy.log(a,b); result=(sympy.simplify(logba-1)==0)"},
{"claim":"Q2: n! eventually exceeds 2^n for n>=4",
"code":"n=sympy.symbols('n'); vals=[sympy.factorial(k) > 2**k for k in range(4,10)]; result=all(bool(v) for v in vals)"},
{"claim":"Q12: 5n^2+3n <= 8n^2 for all n>=1",
"code":"n=sympy.symbols('n', positive=True); diff=8*n**2-(5*n**2+3*n); checks=[diff.subs(n,k)>=0 for k in range(1,20)]; result=all(bool(c) for c in checks)"},
{"claim":"Q14: for n>=2, n*log(n) < n^2 (n log n asymptotically smaller)",
"code":"import math; checks=[k*math.log(k) < k*k for k in range(2,50)]; result=all(checks)"}
]