Recursion Exam
Recursion Exam
1. Factorial Calculation
Write a recursive function to compute the factorial of a number n (n! = n × (n − 1) × · · · × 1)
2. Sum of Digits
Create a function that recursively calculates the sum of digits of a positive integer (e.g., sum digits(123) → 6)
3. Count Down
Implement a recursive countdown from n to 1 that prints each number
4. Power Function
Write a recursive function to calculate x raised to the power y (xy )
5. Fibonacci Sequence
Implement the Fibonacci sequence recursively (fib(0) = 0, fib(1) = 1, fib(n) = fib(n − 1) + fib(n − 2))
7. Binary Search
Implement binary search recursively on a sorted array
8. GCD Calculation
Write a recursive function to find the greatest common divisor (GCD) of two numbers using Euclid’s algorithm
9. Tower of Hanoi
Solve the Tower of Hanoi problem recursively for n disks
10. Linked List Traversal
Write recursive functions to: (a) print all elements, (b) reverse a linked list
1
18. Koch Snowflake
Generate Koch snowflake fractal patterns using recursion
19. Memoization Optimization
Take a naive recursive Fibonacci implementation and optimize it with memoization
20. Mutual Recursion
Implement the Hofstadter Female and Male sequences using mutual recursion
Special Categories
Tree Problems (Natural Recursion Domain)
21. Tree Depth
Calculate the maximum depth of a binary tree recursively
Mathematical Problems
25. Digital Root
Compute the digital root (recursive sum until single digit) of a number
26. Pascal’s Triangle
Generate Pascal’s triangle recursively and find specific values
String Manipulation
28. String Permutations
Generate all unique permutations of a string with duplicate characters
29. Generate Parentheses
Generate all valid combinations of n pairs of parentheses
30. Wildcard Matching
Implement a simple wildcard pattern matching with ’ ?’ and ’*’ using recursion