Complexity Analysis Questions with Answers

99 practice questions on Complexity Analysis (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 99 free in the AptiNest app.

Sample questions

Question 1Easy

What is the final value of count when n = 20?

count = 0
for i in range(n):
    for j in range(i):
        count += 1
  1. 210
  2. 190
  3. 400
  4. 380

Answer: B. 190

Explanation

The inner loop runs i times, so count =
0 + 1 + ... + (n-1)
= n(n-1)/2
= 190.

Step-by-step solution

  1. For i = 0..19 the inner loop runs i times.
  2. count = 0 + 1 + ... + 19.
  3. = 20 x 19 / 2 = 190.

Why the other options are wrong

  • A. 210 = n(n+1)/2: counts one extra per row.
  • C. 400 = n^2: full square, not a triangle.
  • D. 380 = n(n-1): forgot to divide by 2.

Tip: range(i) inside range(n) gives n(n-1)/2.

Question 2Easy

What is the solution of the recurrence T(n) = T(n/2) + 1?

  1. O(n)
  2. O(1)
  3. O(log n)
  4. O(sqrt(n))

Answer: C. O(log n)

Explanation

Each step halves n and does constant work, so there are about log2 n levels: Theta(log n), the binary search recurrence.

Step-by-step solution

  1. One call on n/2 plus constant work.
  2. n halves until 1: log2 n levels.
  3. T(n) = Theta(log n).

Why the other options are wrong

  • A. Linear needs n to drop by a constant each time.
  • B. The depth grows with n.
  • D. sqrt(n) does not come from halving.

Tip: Halve and do constant work: log n.

Question 3Easy

What is the solution of the recurrence T(n) = 2T(n-1) + 1?

  1. O(n)
  2. O(n^2)
  3. O(n 2^n)
  4. O(2^n)

Answer: D. O(2^n)

Explanation

The number of calls doubles with each level, so T(n)
= 2^n - 1
= Theta(2^n).

Step-by-step solution

  1. Each call makes two calls on n - 1.
  2. Level k has 2^k calls; there are n levels.
  3. Total 2^n - 1: Theta(2^n).

Why the other options are wrong

  • A. Linear needs only one call.
  • B. n^2 needs one call with linear work.
  • C. n 2^n would need linear work per call.

Tip: 2T(n-1) + 1 = 2^n - 1.

Question 4Easy

What is the solution of the recurrence T(n) = T(n-1) + 1?

  1. O(log n)
  2. O(1)
  3. O(n)
  4. O(n^2)

Answer: C. O(n)

Explanation

Each level does constant work and there are n levels, so T(n) = Theta(n).

Step-by-step solution

  1. Each level does constant work.
  2. There are n levels (n, n-1, ..., 1).
  3. T(n) = Theta(n).

Why the other options are wrong

  • A. log n needs n to be halved.
  • B. The recursion depth grows with n.
  • D. n^2 needs linear work per level.

Tip: T(n-1) + 1: linear.

Question 5Medium

How many times is f called in total (including the first call) when f(8) is invoked?

def f(n):
    if n <= 1:
        return
    f(n // 2)
    f(n // 2)
  1. 7
  2. 16
  3. 15
  4. 4

Answer: C. 15

Explanation

Level d of the recursion tree has 2^d calls and there are log2(8) + 1 levels, so the total is 2*8 - 1 = 15.

This T(n) = 2T(n/2) + O(1) pattern is O(n).

Step-by-step solution

  1. f(8) calls f(4) twice; each f(4) calls f(2) twice; each f(2) calls f(1) twice.
  2. Calls per level: 1, 2, 4, 8.
  3. Total = 15 = 2 x 8 - 1.

Why the other options are wrong

  • A. 7 omits the leaf calls f(1).
  • B. 16 forgets the minus one.
  • D. 4 counts levels, not calls.

Tip: Full binary recursion tree with n leaves has 2n - 1 nodes.

Question 6Medium

What is the final value of count when n = 11?

count = 0
for i in range(n):
    for j in range(n):
        if j == i:
            break
        count += 1
  1. 110
  2. 66
  3. 121
  4. 55

Answer: D. 55

Explanation

For each i the inner loop counts j = 0..i-1 and breaks at j = i, so count =
0 + 1 + ... + (n-1)
= 55.

Step-by-step solution

  1. Row i contributes i (the break fires at j = i).
  2. count = 0 + 1 + ... + 10.
  3. = 55.

Why the other options are wrong

  • A. 110 = n(n-1), forgetting to halve.
  • B. 66 = n(n+1)/2 counts one extra per row.
  • C. 121 = n^2 ignores the break.

Tip: break at j == i makes the inner loop run i times.

Question 7Medium

What is the solution of the recurrence
T(n) = 4T(n/2) + n^2?

  1. O(n^2)
  2. O(n^2 log n)
  3. O(n^3)
  4. O(n log n)

Answer: B. O(n^2 log n)

Explanation

n^(log2 4) = n^2 equals f(n), so case 2 gives Theta(n^2 log n).

Step-by-step solution

  1. a = 4, b = 2; n^(log2 4) = n^2.
  2. f(n) = n^2 matches: case 2.
  3. T(n) = Theta(n^2 log n).

Why the other options are wrong

  • A. A tie in case 2 adds a log factor.
  • C. n^3 needs a = 8.
  • D. n log n needs a = 2 and f(n) = n.

Tip: Tie: multiply by log n.

Question 8Medium

What is the solution of the recurrence
T(n) = 7T(n/2) + n^2?

  1. O(n^2 log n)
  2. O(n^2.81)
  3. O(n^3)
  4. O(n^2)

Answer: B. O(n^2.81)

Explanation

n^(log2 7) ~ n^2.81 dominates n^2, so case 1 applies.
This is Strassen's recurrence.

Step-by-step solution

  1. a = 7, b = 2; n^(log2 7) is about n^2.81.
  2. f(n) = n^2 is smaller: case 1.
  3. T(n) = Theta(n^log2 7), about n^2.81 (Strassen).

Why the other options are wrong

  • A. n^2 log n would need a = 4.
  • C. n^3 would need a = 8.
  • D. n^2 would need f(n) to dominate.

Tip: Strassen: 7T(n/2) + n^2 = n^2.81.

Question 9Hard

What is the final value of count when n = 16?

count = 0
for i in range(n):
    k = i
    while k > 0:
        k = k // 2
        count += 1
  1. 48
  2. 32
  3. 64
  4. 49

Answer: D. 49

Explanation

For each i, the while loop runs once per bit of i (floor(log2 i) + 1 times for i >= 1); the sum over i = 0..n-1 is 49.

Step-by-step solution

  1. Bit lengths for i = 0..15: 0; 1; 2, 2; 3 x 4; 4 x 8.
  2. Sum = 0 + 1 + 4 + 12 + 32.
  3. = 49.

Why the other options are wrong

  • A. 48 is one short; check i = 1 contributes 1.
  • B. 32 counts only the 4-bit numbers.
  • C. 64 = n log2 n is an upper estimate.

Tip: Numbers with b bits: 2^(b-1) of them.

Question 10Hard

What is the final value of count when n = 15?

count = 0
j = 0
for i in range(n):
    while j < n and j <= 2 * i:
        j += 1
        count += 1
  1. 15
  2. 225
  3. 30
  4. 120

Answer: A. 15

Explanation

j never decreases and stops at n, so the inner body runs at most n times in total across all outer iterations: count = 15.

Step-by-step solution

  1. j is shared across outer iterations and never goes back.
  2. It stops at n = 15.
  3. So count = 15.

Why the other options are wrong

  • B. 225 treats it as a full n x n loop.
  • C. 30 assumes j can go up to 2n.
  • D. 120 treats it as triangular.

Tip: Count how far the inner pointer moves overall, not per outer step.

Practise all 99 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics