0% found this document useful (0 votes)
2 views1 page

Top 50 Python Recursion Questions Solutions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Top 50 Python Recursion Questions Solutions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Top 50 Python Recursion Interview Questions - Solutions

1. Factorial of a number
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)

2. Sum of first n natural numbers


def sum_n(n):
if n == 0:
return 0
return n + sum_n(n - 1)

3. Print numbers from n to 1 (decreasing order)


def print_decreasing(n):
if n == 0:
return
print(n)
print_decreasing(n - 1)

4. Print numbers from 1 to n (increasing order)


def print_increasing(n):
if n == 0:
return
print_increasing(n - 1)
print(n)

5. Check if a number is a palindrome (using recursion)


def is_palindrome_number(n, temp=None):
if temp is None:
temp = n

def reverse(num):
if num == 0:
return 0
return int(str(num % 10) + str(reverse(num // 10)))

return temp == reverse(n)

You might also like