190 likes | 202 Views
This presentation is based on recursion problems that are most commonly asked in coding interviews. This video is dedicated to helping the candidates with commonly asked recursion interview questions along with their solutions. Learning these questions would definitely help you to bag your dream job. The slide covers the following topics.<br><br>1. Introduction<br>2. sum of numbers from zero to a given number<br>3. factorial of a given number<br>4. nth fibonacci number<br>5. checks if a string is a palindrome<br>6. reverse a given string
E N D
AGENDA Calculate the sum of all numbers from zero to a given number. A recursive function that returns the factorial of given number. A recursive function that returns nth no, in Fibonacci Number. A recursive function that checks if the string is a palindrome. A recursive function that takes a string and reverse the string.
1 A recursive function to calculate the sum of all numbers from zero to a given number.
1 A recursive function to calculate the sum of all numbers from zero to a given number. def sum(int num):if num is 0 or 1return numelsereturn num + sum(num-1)
2 A recursive function that returns the factorial of given number.
2 A recursive function that returns the factorial of given number. def factorial(int n):assert n >=0 and int(n) == n,if n <= 1:return 1else:return n * factorial(n-1) =4
2 A recursive function that returns the factorial of given number. def factorial(int n):assert n >=0 and int(n) == n,if n <= 1:return 1else:return n * factorial(n-1) =4 =12 X
2 A recursive function that returns the factorial of given number. def factorial(int n):assert n >=0 and int(n) == n,if n <= 1:return 1else:return n * factorial(n-1) =4 =12 X =24 X X
2 A recursive function that returns the factorial of given number. def factorial(int n):assert n >=0 and int(n) == n,if n <= 1:return 1else:return n * factorial(n-1) =4 =12 X =24 X X =24 X X X
3 A recursive function that takes a number ‘n’ and returns the nth number of the Fibonacci number.
3 • A recursive function that takes a number ‘n’ and returns the nth number of the Fibonacci number. def fibonacci(int n):if n <= 1:return nelse:return fibonacci(n-1) + fibonacci(n-2)
4 A recursive function that takes a string and returns if the string is a palindrome.
4 A recursive function that takes a string and returns if the string is a palindrome. def ispalin (string str, int low, int high):if low>=high:return Trueif str[low] != str[high] :return Falsereturn ispalin (str,low + 1, high - 1)
5 A recursive function that takes a string and reverse the string.
5 A recursive function that takes a string and reverse the string. def reverse(string str, int low, int high):if low < highswap(str[low],str[high]reverse(str, low+1, high-1)
5 A recursive function that takes a string and reverse the string. def reverse(string str, int low, int high):if low < highswap(str[low],str[high]reverse(str, low+1, high-1)