Recursion Practice
Solve problems using recursive functions with base cases and recurrence.
1
Factorial Using Recursion
Easy
Write a recursive function to compute the factorial of a number.
Input Format
A single line containing integer n.
Output Format
Print the factorial of n.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
120 |
|
Sample Input 2
6 |
Sample Output 2
720 |
|
Sample Input 3
3 |
Sample Output 3
6 |
2
Sum of First N Natural Numbers
Easy
Write a recursive function to return the sum of the first n natural numbers.
Input Format
A single line containing integer n.
Output Format
Print the sum.
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
55 |
|
Sample Input 2
5 |
Sample Output 2
15 |
|
Sample Input 3
100 |
Sample Output 3
5050 |
3
Print Numbers 1 to N Recursively
Easy
Write a recursive function that prints numbers from 1 to n, each on a new line.
Input Format
A single line containing integer n.
Output Format
Print the numbers from 1 to n, one per line.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
1 2 3 4 5 |
|
Sample Input 2
3 |
Sample Output 2
1 2 3 |
|
Sample Input 3
1 |
Sample Output 3
1 |
4
Fibonacci Number
Easy
Write a recursive function to return the nth Fibonacci number (0-indexed).
Input Format
A single line containing integer n.
Output Format
Print the nth Fibonacci number.
Sample Test Cases
|
Sample Input 1
6 |
Sample Output 1
8 |
|
Sample Input 2
10 |
Sample Output 2
55 |
|
Sample Input 3
1 |
Sample Output 3
1 |
5
Sum of Digits Recursively
Easy
Write a recursive function to return the sum of digits of a number.
Input Format
A single line containing integer n.
Output Format
Print the sum of digits.
Sample Test Cases
|
Sample Input 1
1234 |
Sample Output 1
10 |
|
Sample Input 2
111 |
Sample Output 2
3 |
|
Sample Input 3
0 |
Sample Output 3
0 |
6
Power of a Number
Easy
Write a recursive function to compute x raised to the power n.
Input Format
A single line containing two space-separated integers x and n.
Output Format
Print x^n.
Sample Test Cases
|
Sample Input 1
2 10 |
Sample Output 1
1024 |
|
Sample Input 2
3 4 |
Sample Output 2
81 |
|
Sample Input 3
5 0 |
Sample Output 3
1 |
7
Reverse a String Recursively
Easy
Write a recursive function to reverse a string.
Input Format
A single line containing a string S.
Output Format
Print the reversed string.
Sample Test Cases
|
Sample Input 1
hello |
Sample Output 1
olleh |
|
Sample Input 2
world |
Sample Output 2
dlrow |
|
Sample Input 3
abc |
Sample Output 3
cba |
8
Count Digits Recursively
Easy
Write a recursive function to count the number of digits in a positive integer.
Input Format
A single line containing integer n.
Output Format
Print the number of digits.
Sample Test Cases
|
Sample Input 1
98765 |
Sample Output 1
5 |
|
Sample Input 2
100 |
Sample Output 2
3 |
|
Sample Input 3
0 |
Sample Output 3
1 |
9
Check Palindrome String Recursively
Easy
Write a recursive function to check whether a string is a palindrome.
Input Format
A single line containing a string S.
Output Format
Print YES if palindrome, else NO.
Sample Test Cases
|
Sample Input 1
racecar |
Sample Output 1
YES |
|
Sample Input 2
level |
Sample Output 2
YES |
|
Sample Input 3
python |
Sample Output 3
NO |
10
Print n Stars Recursively
Easy
Write a recursive function to print n asterisks (*) on a single line.
Input Format
A single line containing integer n.
Output Format
Print n asterisks.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
***** |
|
Sample Input 2
3 |
Sample Output 2
*** |
|
Sample Input 3
1 |
Sample Output 3
* |
1
GCD Using Recursion
Medium
Write a recursive function to compute the GCD of two numbers using the Euclidean algorithm.
Input Format
A single line containing two space-separated integers.
Output Format
Print the GCD.
Sample Test Cases
|
Sample Input 1
48 18 |
Sample Output 1
6 |
|
Sample Input 2
54 24 |
Sample Output 2
6 |
|
Sample Input 3
100 10 |
Sample Output 3
10 |
2
Count Occurrences in a List Recursively
Medium
Write a recursive function to count how many times a target value appears in a list.
Input Format
First line: integer N. Second line: N space-separated integers. Third line: target value.
Output Format
Print the count of the target value.
Sample Test Cases
|
Sample Input 1
7 1 2 3 2 4 2 5 2 |
Sample Output 1
3 |
|
Sample Input 2
5 1 1 1 1 1 1 |
Sample Output 2
5 |
|
Sample Input 3
3 1 2 3 4 |
Sample Output 3
0 |
3
Sum of Array Recursively
Medium
Write a recursive function to return the sum of all elements in an array.
Input Format
First line: integer N. Second line: N space-separated integers.
Output Format
Print the sum.
Sample Test Cases
|
Sample Input 1
5 1 2 3 4 5 |
Sample Output 1
15 |
|
Sample Input 2
4 10 20 30 40 |
Sample Output 2
100 |
|
Sample Input 3
1 5 |
Sample Output 3
5 |
4
Decimal to Binary
Medium
Write a recursive function to convert a decimal number to its binary representation.
Input Format
A single line containing integer n.
Output Format
Print the binary representation.
Sample Test Cases
|
Sample Input 1
13 |
Sample Output 1
1101 |
|
Sample Input 2
255 |
Sample Output 2
11111111 |
|
Sample Input 3
10 |
Sample Output 3
1010 |
5
Tower of Hanoi
Medium
Write a recursive function to solve the Tower of Hanoi problem and print the moves for n disks.
Input Format
A single line containing integer n (number of disks).
Output Format
Print each move as 'Move disk d from A to C' style lines.
Sample Test Cases
|
Sample Input 1
2 |
Sample Output 1
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C |
|
Sample Input 2
1 |
Sample Output 2
Move disk 1 from A to C |
|
Sample Input 3
3 |
Sample Output 3
Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to C |
6
Check if Array is Sorted Recursively
Medium
Write a recursive function to check whether an array is sorted in non-decreasing order.
Input Format
First line: integer N. Second line: N space-separated integers.
Output Format
Print YES if sorted, else NO.
Sample Test Cases
|
Sample Input 1
5 1 3 5 7 9 |
Sample Output 1
YES |
|
Sample Input 2
3 3 2 1 |
Sample Output 2
NO |
|
Sample Input 3
4 1 2 2 3 |
Sample Output 3
YES |
7
Nth Term of a Recurrence
Medium
Write a recursive function to compute the nth term where term(n) = term(n-1) + term(n-2), with term(0)=0, term(1)=1.
Input Format
A single line containing integer n.
Output Format
Print the nth term.
Sample Test Cases
|
Sample Input 1
9 |
Sample Output 1
34 |
|
Sample Input 2
10 |
Sample Output 2
55 |
|
Sample Input 3
7 |
Sample Output 3
13 |
8
Find Maximum in Array Recursively
Medium
Write a recursive function to find the maximum element in an array.
Input Format
First line: integer N. Second line: N space-separated integers.
Output Format
Print the maximum element.
Sample Test Cases
|
Sample Input 1
6 4 9 2 7 5 1 |
Sample Output 1
9 |
|
Sample Input 2
5 8 3 9 1 7 |
Sample Output 2
9 |
|
Sample Input 3
2 -1 -2 |
Sample Output 3
-1 |
9
String Length Recursively
Medium
Write a recursive function to find the length of a string without using len().
Input Format
A single line containing a string S.
Output Format
Print the length of S.
Sample Test Cases
|
Sample Input 1
programming |
Sample Output 1
11 |
|
Sample Input 2
abc |
Sample Output 2
3 |
|
Sample Input 3
|
Sample Output 3
0 |
10
Generate Fibonacci Series Recursively
Medium
Write a recursive function that prints the first n Fibonacci numbers.
Input Format
A single line containing integer n.
Output Format
Print the first n Fibonacci numbers space-separated.
Sample Test Cases
|
Sample Input 1
7 |
Sample Output 1
0 1 1 2 3 5 8 |
|
Sample Input 2
5 |
Sample Output 2
0 1 1 2 3 |
|
Sample Input 3
1 |
Sample Output 3
0 |
1
N-Queens Problem
Hard
Write a recursive backtracking program to count the number of ways to place n queens on an n x n board so that no two attack each other.
Input Format
A single line containing integer n.
Output Format
Print the number of valid arrangements.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
2 |
|
Sample Input 2
5 |
Sample Output 2
10 |
|
Sample Input 3
1 |
Sample Output 3
1 |
2
Permutations of a String
Hard
Write a recursive function to generate all permutations of the given string.
Input Format
A single line containing a string S.
Output Format
Print each permutation on a new line, in any order.
Sample Test Cases
|
Sample Input 1
abc |
Sample Output 1
abc acb bac bca cab cba |
|
Sample Input 2
ab |
Sample Output 2
ab ba |
|
Sample Input 3
a |
Sample Output 3
a |
3
Subset Sum Count
Hard
Write a recursive function to count the number of subsets of an array whose sum equals a target.
Input Format
First line: integer N. Second line: N space-separated integers. Third line: target sum.
Output Format
Print the count of subsets.
Sample Test Cases
|
Sample Input 1
5 1 2 3 4 5 7 |
Sample Output 1
3 |
|
Sample Input 2
4 1 2 3 4 5 |
Sample Output 2
2 |
|
Sample Input 3
3 1 2 3 3 |
Sample Output 3
2 |
4
Combinations of K Elements
Hard
Write a recursive function to print all combinations of k elements from an array of n distinct integers.
Input Format
First line: N and k space-separated. Second line: N space-separated integers.
Output Format
Print each combination on a new line, elements space-separated.
Sample Test Cases
|
Sample Input 1
4 2 1 2 3 4 |
Sample Output 1
1 2 1 3 1 4 2 3 2 4 3 4 |
|
Sample Input 2
3 2 1 2 3 |
Sample Output 2
1 2 1 3 2 3 |
|
Sample Input 3
2 1 9 8 |
Sample Output 3
9 8 |
5
Knight's Tour Count
Hard
Write a recursive program to count the number of closed tours a knight can make on a small n x n board starting from (0,0).
Input Format
A single line containing integer n (2 ≤ n ≤ 5).
Output Format
Print the number of closed knight tours.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
1728 |
|
Sample Input 2
4 |
Sample Output 2
0 |
|
Sample Input 3
3 |
Sample Output 3
0 |
6
Palindrome Partitioning
Hard
Write a recursive function to print all ways to partition a string such that every part is a palindrome.
Input Format
A single line containing a string S.
Output Format
Print each partition on a new line, parts space-separated.
Sample Test Cases
|
Sample Input 1
aab |
Sample Output 1
a a b aa b |
|
Sample Input 2
aab |
Sample Output 2
a a b aa b |
|
Sample Input 3
aba |
Sample Output 3
a b a aba |
7
Generate All Balanced Parentheses
Hard
Write a recursive function to generate all combinations of n pairs of balanced parentheses.
Input Format
A single line containing integer n.
Output Format
Print each valid combination on a new line.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
((())) (()()) (())() ()(()) ()()() |
|
Sample Input 2
2 |
Sample Output 2
(()) ()() |
|
Sample Input 3
1 |
Sample Output 3
() |
8
Binary Search Recursive
Hard
Write a recursive binary search function that returns the index of a target in a sorted array, or -1.
Input Format
First line: integer N. Second line: N space-separated sorted integers. Third line: target.
Output Format
Print the index (0-based), or -1.
Sample Test Cases
|
Sample Input 1
6 1 3 5 7 9 11 7 |
Sample Output 1
3 |
|
Sample Input 2
5 1 2 3 4 5 5 |
Sample Output 2
4 |
|
Sample Input 3
5 1 2 3 4 5 0 |
Sample Output 3
-1 |
9
Maze Paths Count
Hard
Write a recursive function to count the number of ways to reach the bottom-right cell of an m x n grid moving only right or down.
Input Format
A single line containing two space-separated integers m and n.
Output Format
Print the number of paths.
Sample Test Cases
|
Sample Input 1
3 3 |
Sample Output 1
6 |
|
Sample Input 2
2 3 |
Sample Output 2
3 |
|
Sample Input 3
1 1 |
Sample Output 3
1 |
10
Tower of Hanoi with Count
Hard
Write a recursive program to solve the Tower of Hanoi for n disks and also print the total number of moves.
Input Format
A single line containing integer n.
Output Format
Print the moves first (one per line), then 'Total moves: [count]'.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to C Total moves: 7 |
|
Sample Input 2
2 |
Sample Output 2
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C Total moves: 3 |
|
Sample Input 3
1 |
Sample Output 3
Move disk 1 from A to C Total moves: 1 |
Competitive MCQs — Recursion
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 34
Q1
What is a recursive function?
Correct!
Wrong — correct answer is .
Recursion is when a function calls itself to solve smaller subproblems.
Q2
What is the output of this code?
python
1
2
3
4
5
2
3
4
5
def fact(n):
if n <= 1:
return 1
return n * fact(n - 1)
print(fact(5))Correct!
Wrong — correct answer is .
fact(5) = 5 × 4 × 3 × 2 × 1 = 120.
Q3
What is the output of this code?
def fact(n):
if n <= 1:
return 1
return n * fact(n - 1)
print(fact(5))
def fact(n):
if n <= 1:
return 1
return n * fact(n - 1)
print(fact(5))
Correct!
Wrong — correct answer is .
fact(5) = 5 × 4 × 3 × 2 × 1 = 120.
Q4
What is the base case in a recursive function?
Correct!
Wrong — correct answer is .
A base case prevents infinite recursion by providing a stopping condition.
Q5
What happens if a recursive function has no base case?
Correct!
Wrong — correct answer is .
Unbounded recursion consumes the call stack and raises RecursionError.
Q6
What is the output of this code?
python
1
2
3
4
5
2
3
4
5
def count(n):
if n == 0:
return 0
return 1 + count(n - 1)
print(count(4))Correct!
Wrong — correct answer is .
Each call adds 1 until n hits 0: 1+1+1+1 = 4.
Q7
What is the output of this code?
def count(n):
if n == 0:
return 0
return 1 + count(n - 1)
print(count(4))
def count(n):
if n == 0:
return 0
return 1 + count(n - 1)
print(count(4))
Correct!
Wrong — correct answer is .
Each call adds 1 until n hits 0: 1+1+1+1 = 4.
Q8
Which series is classically defined recursively as F(n) = F(n-1) + F(n-2)?
Correct!
Wrong — correct answer is .
The Fibonacci sequence is the classic recursive example.
Q9
What is the output of this code?
python
1
2
3
4
5
6
2
3
4
5
6
def greet(n):
if n <= 0:
return
print("Hi")
greet(n - 1)
greet(3)Correct!
Wrong — correct answer is .
greet(3) prints Hi and calls greet(2), which prints and calls greet(1)... 3 lines.
Q10
What is the output of this code?
def greet(n):
if n <= 0:
return
print("Hi")
greet(n - 1)
greet(3)
def greet(n):
if n <= 0:
return
print("Hi")
greet(n - 1)
greet(3)
Correct!
Wrong — correct answer is .
greet(3) prints Hi and calls greet(2), which prints and calls greet(1)... 3 lines.
Q11
What is the maximum recursion depth problem in Python called?
Correct!
Wrong — correct answer is .
Python raises RecursionError when the recursion limit is exceeded.
Q12
What is the output of this code?
python
1
2
3
4
5
2
3
4
5
def power(b, e):
if e == 0:
return 1
return b * power(b, e - 1)
print(power(2, 3))Correct!
Wrong — correct answer is .
2 × 2 × 2 = 8; power(2,3) multiplies 2 three times.
Q13
What is the output of this code?
def power(b, e):
if e == 0:
return 1
return b * power(b, e - 1)
print(power(2, 3))
def power(b, e):
if e == 0:
return 1
return b * power(b, e - 1)
print(power(2, 3))
Correct!
Wrong — correct answer is .
2 × 2 × 2 = 8; power(2,3) multiplies 2 three times.
Q14
Every recursive solution can also be written using which construct?
Correct!
Wrong — correct answer is .
Any recursive function can be rewritten iteratively with loops and a stack.
Q15
What is the output of this code?
def f(n):
if n == 0:
return 0
return n + f(n - 1)
print(f(3))
def f(n):
if n == 0:
return 0
return n + f(n - 1)
print(f(3))
Correct!
Wrong — correct answer is .
f(3) = 3 + 2 + 1 + 0 = 6.
Q16
What is the output of this code?
def rev(s):
if s == "":
return ""
return rev(s[1:]) + s[0]
print(rev("abc"))
def rev(s):
if s == "":
return ""
return rev(s[1:]) + s[0]
print(rev("abc"))
Correct!
Wrong — correct answer is .
The recursion moves the first character to the end each step, reversing the string.
Q17
How many total calls are made by f(3) in this code?
def f(n):
if n <= 1:
return 1
return f(n - 1) + f(n - 2)
print(f(3))
def f(n):
if n <= 1:
return 1
return f(n - 1) + f(n - 2)
print(f(3))
Correct!
Wrong — correct answer is .
f(3) calls f(2) and f(1); f(2) calls f(1) and f(0) → 5 calls including the initial one.
Q18
What happens when recursion never reaches its base case?
Correct!
Wrong — correct answer is .
Python raises RecursionError once the recursion limit is crossed.
Q19
Which is the best definition of a base case?
Correct!
Wrong — correct answer is .
Base cases stop the chain by returning without another recursive call.
Q20
What is the output of this code?
def ack(m, n):
return m + n
print(ack(2, ack(1, 1)))
def ack(m, n):
return m + n
print(ack(2, ack(1, 1)))
Correct!
Wrong — correct answer is .
Inner ack(1, 1) = 2, so outer ack(2, 2) = 4.
Q21
Which classic problem is naturally recursive?
Correct!
Wrong — correct answer is .
Tower of Hanoi maps directly onto recursive moves.
Q22
What is the output of this code?
def countdown(n):
if n == 0:
return
print(n, end=" ")
countdown(n - 1)
countdown(3)
def countdown(n):
if n == 0:
return
print(n, end=" ")
countdown(n - 1)
countdown(3)
Correct!
Wrong — correct answer is .
The function prints 3, recurses with 2, prints 2, recurses with 1, and stops at 0.
Q23
What is the output of this code?
def power(b, e):
if e == 0:
return 1
return b * power(b, e - 1)
print(power(3, 2))
def power(b, e):
if e == 0:
return 1
return b * power(b, e - 1)
print(power(3, 2))
Correct!
Wrong — correct answer is .
power(3, 2) = 3 × 3 × 1 = 9.
Q24
What is the primary risk of recursion?
Correct!
Wrong — correct answer is .
Each call consumes stack memory; very deep chains overflow it.
Q25
What is the output of this code?
def f(n):
if n == 1:
return 1
return 1 + f(n - 1)
print(f(4))
def f(n):
if n == 1:
return 1
return 1 + f(n - 1)
print(f(4))
Correct!
Wrong — correct answer is .
Four recursive steps each add 1 until n reaches 1, giving 4.
Q26
Which data structure does recursion implicitly use?
Correct!
Wrong — correct answer is .
Function calls are pushed onto the call stack and popped on return.
Q27
What is the output of this code?
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
print(gcd(12, 8))
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
print(gcd(12, 8))
Correct!
Wrong — correct answer is .
Euclid's algorithm: gcd(12, 8) → gcd(8, 4) → gcd(4, 0) = 4.
Q28
What is recursion depth?
Correct!
Wrong — correct answer is .
Depth counts how many calls are currently stacked before returning.
Q29
Which change usually fixes a RecursionError?
Correct!
Wrong — correct answer is .
A correct base case or a larger sys.setrecursionlimit resolves deep recursion.
Q30
What is the output of this code?
def f(n):
if n < 2:
return n
return f(n - 1) + f(n - 2)
print(f(6))
def f(n):
if n < 2:
return n
return f(n - 1) + f(n - 2)
print(f(6))
Correct!
Wrong — correct answer is .
This is Fibonacci: 0, 1, 1, 2, 3, 5, 8 — f(6) is 8.
Q31
Which is TRUE about tail recursion in Python?
Correct!
Wrong — correct answer is .
CPython does not implement tail-call optimization, so deep tail recursion still overflows.
Q32
What is the output of this code?
def f(n):
return 1 if n == 0 else n * f(n - 1)
print(f(0))
def f(n):
return 1 if n == 0 else n * f(n - 1)
print(f(0))
Correct!
Wrong — correct answer is .
f(0) hits the base case immediately and returns 1.
Q33
Which type of problems suits recursion best?
Correct!
Wrong — correct answer is .
Problems like tree traversal and divide-and-conquer mirror recursion naturally.
Q34
What is the output of this code?
def f(n):
if n == 0:
return ""
return "*" + f(n - 1)
print(f(3))
def f(n):
if n == 0:
return ""
return "*" + f(n - 1)
print(f(3))
Correct!
Wrong — correct answer is .
Each call prepends one star; three calls produce "***".