Recursion & Backtracking Questions with Answers

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

Sample questions

Question 1Easy

What does this code print?

def f(n):
    if n == 1:
        return 1
    return n * f(n - 1) // (n - 1) + 1

print(f(5))
  1. 18
  2. 9
  3. 8
  4. 10

Answer: B. 9

Explanation

n * f(n-1) // (n-1) + 1 with f(1) = 1 gives f(2) = 3, f(3) = 5, f(4) = 7, f(5) = 9.

Step-by-step solution

  1. f(1) = 1.
  2. f(2) = 2*1//1 + 1 = 3; f(3) = 3*3//2 + 1 = 5.
  3. f(4) = 4*5//3 + 1 = 7; f(5) = 5*7//4 + 1 = 9.

Why the other options are wrong

  • A. 18 doubles the answer.
  • C. 8 forgets the + 1.
  • D. 10 is one too many: 5 * 7 // 4 = 8 under floor division, so f(5) = 9.

Tip: Apply // at every step, not only at the end.

Question 2Easy

What does this code print?

def f(n):
    if n == 0:
        return 1
    return 3 * f(n - 1)

print(f(4))
  1. 162
  2. 81
  3. 82
  4. 80

Answer: B. 81

Explanation

f(n) = 3^n, so f(4) = 81.

Step-by-step solution

  1. f(0) = 1, and each level multiplies by 3.
  2. f(4) = 3 * 3 * 3 * 3 * 1 = 3^4.
  3. = 81.

Why the other options are wrong

  • A. That is 2 * 81; there is no extra doubling.
  • C. 82 is not what the recursion gives; f(4) = 3^4 = 81 exactly.
  • D. 80 is one less than 3^4; f(0) returns 1, so nothing is subtracted.

Question 3Easy

What is the minimum number of moves needed to solve the Tower of Hanoi with 5 disks?

  1. 16
  2. 31
  3. 25
  4. 32

Answer: B. 31

Explanation

T(n) = 2T(n-1) + 1
gives 2^n - 1 = 31.

Step-by-step solution

  1. T(n) = 2^n - 1.
  2. T(5) = 32 - 1 = 31.

Why the other options are wrong

  • A. 16 is 2^4.
  • C. 25 is 5^2.
  • D. 32 is 2^5, one too many.

Tip: Hanoi moves = 2^n - 1.

Question 4Easy

How many valid (balanced) strings of 3 pairs of parentheses can be generated?

  1. 7
  2. 6
  3. 5
  4. 8

Answer: C. 5

Explanation

This is the Catalan number C(3) = C(6, 3)/4 = 5.

Step-by-step solution

  1. C(3) = 6! / (4! * 3!) = 5.
  2. They are ((())), (()()), (())(), ()(()), ()()().

Why the other options are wrong

  • A. 7 is not a Catalan number.
  • B. 6 overcounts by one.
  • D. 8 is not a Catalan number; only 5 of the sequences are balanced.

Tip: Catalan: 1, 1, 2, 5, 14, 42.

Question 5Medium

What does this code print?

def f(n):
    if n <= 1:
        return n
    return f(n - 1) + 2 * f(n - 2)

print(f(6))
  1. 22
  2. 42
  3. 21
  4. 20

Answer: C. 21

Explanation

f(0..6) = 0, 1, 1, 3, 5, 11, 21, so f(6) = 21.

Step-by-step solution

  1. f(0)=0, f(1)=1.
  2. f(2)=1+0=1, f(3)=1+2=3, f(4)=3+2=5.
  3. f(5)=5+6=11, f(6)=11+10=21.

Why the other options are wrong

  • A. 22 adds an extra 1.
  • B. 42 doubles the answer.
  • D. 20 is off by one in the last addition.

Tip: Build a small table from the base cases upward.

Question 6Medium

What does this code print?

def f(n):
    if n == 0:
        print(0, end=" ")
        return
    print(n, end=" ")
    f(n - 1)
    print(n, end=" ")

f(3)
  1. 3 2 1 0 1 2 3
  2. 3 2 1 0
  3. 3 2 1 0 0 1 2 3
  4. 0 1 2 3 3 2 1

Answer: A. 3 2 1 0 1 2 3

Explanation

Each call prints n before and after recursing, so the output is 3 2 1 0 1 2 3.

Step-by-step solution

  1. f(3) prints 3, calls f(2), then prints 3 again.
  2. f(2) prints 2, f(1), 2; f(1) prints 1, f(0), 1; f(0) prints 0.
  3. Result: 3 2 1 0 1 2 3.

Why the other options are wrong

  • B. This forgets the prints after the recursive call returns.
  • C. 0 is printed only once, since f(0) returns early.
  • D. The first print happens before recursing, so 3 comes first.

Tip: Before call = descending, after call = ascending.

Question 7Medium

What does this code print?

def f(a, b):
    if b == 0:
        return a
    return f(b, a % b)

print(f(252, 105))
  1. 21
  2. 20
  3. 42
  4. 22

Answer: A. 21

Explanation

This is Euclid's GCD: (252, 105) -> (105, 42) -> (42, 21) -> (21, 0), giving 21.

Step-by-step solution

  1. f(252, 105) -> f(105, 42) (252 mod 105 = 42).
  2. f(105, 42) -> f(42, 21).
  3. f(42, 21) -> f(21, 0) -> 21.

Why the other options are wrong

  • B. 20 is not a divisor of 105.
  • C. 42 does not divide 105.
  • D. 22 does not divide 252 and 105.

Tip: gcd(a, b) = gcd(b, a % b), stop when b = 0.

Question 8Medium

What is the maximum recursion depth (number of active frames) when computing fib(10) with naive recursion fib(n) = fib(n-1) + fib(n-2), fib(0) = 0, fib(1) = 1?

  1. 177
  2. 55
  3. 10
  4. 5

Answer: C. 10

Explanation

Following the fib(n-1) branch down from fib(10) to fib(1) gives 10 frames on the stack at once; the total number of calls (177) is much larger.

Step-by-step solution

  1. Depth is the longest chain of active calls.
  2. The chain fib(10) -> fib(9) -> ... -> fib(1) is the longest.
  3. That is 10 frames.

Why the other options are wrong

  • A. 177 is the total number of calls, not the depth.
  • B. 55 is fib(10), the value.
  • D. 5 would follow only the n - 2 branch.

Tip: Time = total calls; space = deepest chain.

Question 9Hard

What does this code print?

def f(a, n):
    if n == 1:
        return a[0]
    return max(a[n - 1], f(a, n - 1)) - 1

print(f([3, 8, 2, 6], 4))
  1. 6
  2. 5
  3. 10
  4. 4

Answer: B. 5

Explanation

f(1) = 3, f(2) = max(8, 3) - 1 = 7, f(3) = max(2, 7) - 1 = 6, f(4) = max(6, 6) - 1 = 5.

Step-by-step solution

  1. Base case: f(a, 1) = a[0] = 3.
  2. f(a, 2) = max(a[1], 3) - 1 = max(8, 3) - 1 = 7.
  3. f(a, 3) = max(a[2], 7) - 1 = max(2, 7) - 1 = 6.
  4. f(a, 4) = max(a[3], 6) - 1 = max(6, 6) - 1 = 5.

Why the other options are wrong

  • A. Forgets the final - 1 at the top level.
  • C. Ignores the - 1 terms and adds instead of taking max.
  • D. Subtracts one time too many.

Tip: Evaluate recursion bottom-up: start at the base case and work outward.

Question 10Hard

In the subset-sum backtracking search over n items, the worst-case number of nodes visited is:

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

Answer: A. O(2^n)

Explanation

Each item is either included or excluded, giving a binary tree with 2^n leaves when nothing is pruned.

Step-by-step solution

  1. Each of the n items is included or excluded.
  2. The decision tree has up to 2^(n+1) - 1 nodes.
  3. That is O(2^n).

Why the other options are wrong

  • B. n log n is a sorting bound.
  • C. n^2 is too small for an include/exclude tree.
  • D. n! counts orderings, but subsets ignore order.

Tip: Include/exclude per item: 2^n.

Practise all 49 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