Complexity & Optimization MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. What is the auxiliary space complexity of iterative binary search on a sorted array of n elements?

    1. O(log n)
    2. O(n)
    3. O(1)
    4. O(n log n)
    Show answer

    Answer: C. O(1)

    Iterative binary search keeps only a few index variables (low, high, mid), whatever the size of n, so it needs O(1) extra space. The recursive version uses O(log n) stack space, one frame per halving.

  2. Memoising a recursive Fibonacci function is a classic example of which trade-off?

    1. Using O(n) extra memory to cut the running time from exponential to O(n)
    2. Using more time to save memory
    3. Losing accuracy in exchange for speed
    4. Using parallel processors to reduce memory
    Show answer

    Answer: A. Using O(n) extra memory to cut the running time from exponential to O(n)

    Plain recursive Fibonacci recomputes the same subproblems and takes exponential time. Storing each fib(k) once in a table costs O(n) memory but means every value is computed only once, so the time falls to O(n). That is a time-space trade-off: spend memory to save time.

  3. In the master theorem recurrence T(n) = aT(n/b) + f(n), what does a represent?

    1. The factor by which the input shrinks
    2. The cost of dividing and combining
    3. The depth of the recursion tree
    4. The number of subproblems each call creates
    Show answer

    Answer: D. The number of subproblems each call creates

    a is how many recursive calls each problem makes, b is how much smaller each subproblem is (size n/b) and f(n) is the non-recursive work for dividing and combining. Merge sort has a = 2, b = 2 and f(n) = Theta(n).

  4. Which recurrence describes merge sort's running time?

    1. T(n) = T(n/2) + Theta(1)
    2. T(n) = 2T(n/2) + Theta(n)
    3. T(n) = T(n - 1) + Theta(n)
    4. T(n) = 2T(n - 1) + Theta(1)
    Show answer

    Answer: B. T(n) = 2T(n/2) + Theta(n)

    Merge sort splits the array into two halves, sorts each recursively (2T(n/2)) and merges them in linear time (Theta(n)). T(n/2) + Theta(1) is binary search, T(n - 1) + Theta(n) is worst-case quicksort, and 2T(n - 1) + 1 is Tower of Hanoi.

  5. What is the auxiliary space used by a recursive factorial(n) function that calls factorial(n - 1)?

    1. O(1), because it uses no arrays
    2. O(n), because n stack frames are active at the deepest point
    3. O(log n), because the input halves each call
    4. O(n!), because the result grows that fast
    Show answer

    Answer: B. O(n), because n stack frames are active at the deepest point

    Each call waits for factorial(n - 1) to return, so n frames sit on the call stack at once. Space counts the call stack even when no array is declared. The input shrinks by 1, not by half, so it is not O(log n).

  6. A dynamic array doubles its capacity whenever it is full. What is the amortised cost of one append?

    1. O(log n)
    2. O(1)
    3. O(n)
    4. O(n log n)
    Show answer

    Answer: B. O(1)

    A resize copies all current elements, which is O(n), but resizes happen only at sizes 1, 2, 4, 8 and so on. Across n appends the total copying is below 2n, so the average per append over any sequence is O(1) amortised.

  7. In the master theorem, f(n) is compared with which function to decide the case?

    1. n^(log_b a)
    2. n log n
    3. a^n
    4. log_a b
    Show answer

    Answer: A. n^(log_b a)

    n^(log_b a) is the total work done at the leaves of the recursion tree. If f(n) is polynomially smaller, the leaves dominate (case 1). If they match, every level costs the same and a log factor appears (case 2). If f(n) is polynomially larger and regular, the root dominates (case 3).

  8. Quicksort always picks the smallest element as the pivot on an already-sorted array. Which recurrence and bound describe this worst case?

    1. T(n) = T(n - 1) + Theta(n), which solves to Theta(n^2)
    2. T(n) = 2T(n/2) + Theta(n), which solves to Theta(n log n)
    3. T(n) = T(n - 1) + Theta(1), which solves to Theta(n)
    4. T(n) = T(n/2) + Theta(n), which solves to Theta(n)
    Show answer

    Answer: A. T(n) = T(n - 1) + Theta(n), which solves to Theta(n^2)

    With the smallest element as pivot, partitioning costs Theta(n) and leaves one empty side and one side of n - 1 elements. Unrolling gives n + (n - 1) + ... + 1 = n(n + 1)/2, which is Theta(n^2). Balanced splits give the Theta(n log n) average case instead.

  9. What is the difference between f(n) = O(g(n)) and f(n) = Theta(g(n))?

    1. O is for worst-case analysis and Theta is only for average case
    2. O ignores constants but Theta keeps them
    3. O gives only an upper bound, while Theta bounds f from above and below
    4. They mean the same thing in algorithm analysis
    Show answer

    Answer: C. O gives only an upper bound, while Theta bounds f from above and below

    f = O(g) means f grows no faster than g up to a constant, so n = O(n^2) is true. f = Theta(g) means f is both O(g) and Omega(g), a tight bound. Either notation can describe the worst, average or best case, and both ignore constant factors.

  10. Merge sort and heap sort both run in O(n log n) time. How do they compare on auxiliary space for an array?

    1. Both need O(n) extra space
    2. Merge sort needs O(n) extra space; heap sort sorts in place with O(1)
    3. Heap sort needs O(n) extra space; merge sort needs O(1)
    4. Both need O(log n) extra space
    Show answer

    Answer: B. Merge sort needs O(n) extra space; heap sort sorts in place with O(1)

    Standard array merge sort copies into a temporary buffer during merging, so it needs O(n) extra space. Heap sort builds the heap inside the input array and swaps in place, using O(1) extra space. The trade-off is that merge sort is stable and heap sort is not.

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.