Complexity & Optimization MCQs for NSCT
Complexity and optimisation is about measuring how an algorithm's time and memory grow with input size n, solving recurrences for recursive algorithms, and choosing changes that improve the growth rate rather than only the constant. It is topic 12 of the Data Structures & Algorithms area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 40.2% accuracy in this area, per the HEC NSCT report and APP.
Big-O, Omega and Theta are introduced in Foundations of Data Structures and Algorithms; this page builds on them.
Time vs space
Time complexity counts basic steps; space complexity counts extra memory, including the call stack. Many optimisations trade one for the other.
| Problem | Low-memory approach | Faster approach |
|---|---|---|
| Fibonacci | Plain recursion: exponential time (fib(10) makes 177 calls) | Memoisation: O(n) time, O(n) table |
| Pair with a target sum | All pairs: O(n^2), O(1) space | Hash set: O(n) expected, O(n) space |
| Duplicate check | Heap sort then scan: O(n log n), O(1) space | Hash set: O(n) expected, O(n) space |
| Sorting | Heap sort: O(n log n), O(1) space | Merge sort: O(n log n), O(n) space, but stable |
Recursion costs space even without arrays: recursive factorial(n) keeps n frames on the stack, so it uses O(n) space.
Amortised analysis averages the cost over a worst-case sequence of operations, with no probability involved. A dynamic array that doubles when full copies 1 + 2 + 4 + 8 + 16 = 31 elements while appending 17 items, so an append is O(1) amortised. Growing by a fixed 10 slots instead copies 49,500 elements over 1,000 appends, which is quadratic.
Exam angle: loops that run one after another add (n + n = O(n)); nested loops multiply. A doubling inner loop inside an n-loop gives n log2 n, which is 64 for n = 16.
Recurrences
A recurrence expresses the running time of a recursive algorithm in terms of smaller inputs.
| Recurrence | Algorithm | Solution |
|---|---|---|
| T(n) = T(n/2) + 1 | Binary search | Theta(log n) |
| T(n) = 2T(n/2) + n | Merge sort | Theta(n log n) |
| T(n) = T(n - 1) + n | Worst-case quicksort | Theta(n^2) |
| T(n) = 2T(n - 1) + 1 | Tower of Hanoi | Theta(2^n) |
| T(n) = T(n/3) + T(2n/3) + n | Uneven split | Theta(n log n) |
There are three standard methods. Substitution means guessing and proving by induction. The recursion tree sums the cost of each level. The master theorem handles the form T(n) = aT(n/b) + f(n).
Exact values help you check a closed form. With T(1) = 1, T(n) = 2T(n/2) + n gives T(16) = 16 log2 16 + 16 = 80, and the Hanoi recurrence gives T(10) = 2^10 - 1 = 1023.
Exam angle: subtract-and-conquer recurrences such as T(n - 1) are not master-theorem cases; unroll them instead.
Master theorem
Compare f(n) with n^(log_b a), the total work at the leaves:
| Case | Condition | Result | Example |
|---|---|---|---|
| 1 | f(n) polynomially smaller | Theta(n^(log_b a)) | 8T(n/2) + n^2 = Theta(n^3) |
| 2 | f(n) = Theta(n^(log_b a)) | Theta(n^(log_b a) log n) | 4T(n/2) + n^2 = Theta(n^2 log n) |
| 3 | f(n) polynomially larger, and a f(n/b) <= c f(n) for some c < 1 | Theta(f(n)) | 3T(n/4) + n log n = Theta(n log n) |
Strassen's 7T(n/2) + n^2 is case 1, giving Theta(n^(log_2 7)), about n^2.81. Karatsuba's 3T(n/2) + n gives about n^1.585, so doubling the input roughly triples the time.
Where books differ: CLRS says the basic theorem does not cover T(n) = 2T(n/2) + n log n, because n log n is larger than n but not polynomially larger. Many other texts use an extended case 2 (f(n) = Theta(n^(log_b a) log^k n) gives Theta(n^(log_b a) log^(k+1) n)), which yields Theta(n log^2 n).
Exam angle: compute log_b a first, then decide which side wins.
Common traps
- Multiplying sequential loops; only nested loops multiply.
- Ignoring recursion stack space when a question asks for space complexity.
- Applying the master theorem to T(n - 1) recurrences or to uneven splits.
- Forgetting the extra log factor in case 2.
- Assuming a Theta(n log n) algorithm always beats a Theta(n^2) one. With costs 2n^2 and 50 n log2 n, the quadratic one is faster up to n = 189.
- Confusing amortised cost (guaranteed over any sequence) with average-case cost (expected over random inputs).
Worked examples
1. Understanding. In the master theorem, what happens when f(n) and n^(log_b a) grow at the same rate? Answer: case 2 applies, and each of the about log n levels costs the same, so the result gains a log factor: Theta(n^(log_b a) log n).
2. Application. Solve T(n) = 9T(n/3) + n. Answer: Theta(n^2). n^(log_3 9) = n^2, and f(n) = n is polynomially smaller, so case 1 applies. A numerical check gives T(n)/n^2 settling at 1.5.
3. Analysis. An outer loop runs i = 1, 3, 9, ... while i <= n, and an inner loop runs n times for each i. What is the count for n = 27, and what is the complexity? Answer: 108, which is Theta(n log n). i takes the values 1, 3, 9 and 27, four values (log3 27 + 1), and each does 27 inner steps.
Practise
The quiz below has 100 Complexity & Optimization questions, filterable by tier and difficulty.
Practise: 100 questions
FAQs
What is the difference between time complexity and space complexity?
Time complexity describes how the number of basic steps grows with n; space complexity describes how much extra memory is needed, including the recursion stack. They often trade off. A hash set can turn an O(n^2) pair search into O(n) expected time, but it costs O(n) memory.
When can I not use the master theorem?
It applies only to T(n) = aT(n/b) + f(n) with a >= 1 and b > 1. It does not cover subtract-and-conquer forms such as T(n - 1) + n, uneven splits such as T(n/3) + T(2n/3) + n, or the gaps where f(n) is not polynomially larger or smaller. Use substitution or a recursion tree there.
Is amortised O(1) the same as O(1)?
No. Amortised O(1) means a sequence of n operations costs O(n) in total, but one operation can still be slow. A doubling array's append is usually instant, yet the append that triggers a resize copies every element. That suits batch work but can matter in real-time systems.
Next step
For more loop and recurrence drills, go to Big-O Practice, return to the DSA MCQ hub, or test yourself across every area in an NSCT mock test.