Top 15 Recursion Programming Exercises for Java Programmers with Solution

Hello guys, if you are trying to learn recursion but struggling and looking for some common recursive coding problems then you have come to the right place. Earlier, I have shared frequently asked programming interview questions, books, and courses and in this article, I will share 15 coding exercises you can do to learn recursion from scratch. These are common coding problems that can be solved using recursion, these will not only help in learning recursion but also in your coding interview preparation. I personally found the best way to learn Recursion in Java or any programming language is by doing some examples. 

Every programmer knows What is a recursive function or Recursion in Java but when it comes to applying Recursion to a problem, many fumbles. As per my experience, Recursion is a tricky programming concept, some people get it very quickly, but for some programmers, it takes ages to understand Recursion and develop the ability to apply it.

If you fall into the second category of programmers for whom Recursion is always tricky, then the best I can suggest is doing lots of Recursion based programming exercises.

I know, for some people recursion is very tough to master, I was one of them but when you keep solving problems like this, from easy to difficult, you will slowly understand how recursive solution works and come up with a recursion solution for any new coding problem.

Recursion will also help you to solve dynamic programming-based coding problems, which is very important from a coding interview perspective. Recursions are also one of the most powerful techniques to solve linked lists and binary tree-based problems because both linked lists and binary trees are recursive data structures. 

In this Java tutorial, we will see some Recursion based example programs written in the Java programming language. You can also use this as a Java programming exercise. 

By the way, if you struggle to understand recursion patterns and recursion in general, I highly recommend going through these best Recursion courses for programmers to learn how to identity recursive problems and how to break them down to solve using recursion. 





What is Recursion?

Just a recap of what we have discussed about Recursion in Java in our earlier article, Recursion means calling himself. A function or method is said to be Recursion if it calls itself. To write a recursion function, the first thing anyone needs is to find the base case.

The base case is a particular case that can be solved without calling a recursive function. The base case is the endpoint for recursive function, it's the point from where the stack starts winding up. Without any base case, the recursive function will result in StackOverFlowError.

So whenever you need to write a recursive function, first write a base case. Now let's see a couple of examples of base cases in a recursive function. 

If you are calculating factorial than factorial(1) = 1 is a base case, for Fibonacci numbers its f(1) and f(2), for power(int number) its power(0) which is equal to 1. See, all these base cases are conditions that can be solved without calling recursive functions.

15 coding problems to learn Recursion in Java




How to find if a condition can be solved using Recursion?

Finding whether a programming problem can be solved using Recursion is a skill, not everybody sees issues in terms of Recursion. The best thing is to break the problem into a smaller set and see if the smaller problem is the same as the original problem or not like in order to calculate a factorial of 5, does it helps to calculate a factorial of 4? This may be a guide to see if Recursion can be used or not.

Similarly, String questions, Array-based problems, linked list algorithms, binary tree algorithms, and dynamic programming-based coding problems are good candidates for Recursion in Java. 

Since a smaller linked list is a linked list, a smaller tree is a tree itself, problems like reversing the linked list, traversing the tree, etc. can be solved using Recursion in Java.


Steps to solve a problem using Recursion

Once you have identified that a coding problem can be solved using Recursion, You are just two steps away from writing a recursive function.

1.  Find the base case
2.  Finding how to call the method and what to do with the return value.

As discussed above, finding a base case for any recursive solution is the first step towards writing a recursive function in Java or any other programming language. This is usually the simplest way to solve the problem without using recursion. 

For example, when you calculate factorial, the base case is factorial(0) which is 1, you mean you know the answer so you can directly return it and from there onwards recursion will unroll and calculate factorial for the given number. 

Once you have done that, you need to find a way to call the recursive method and what to do with the result returned by the method, sometime you may need to add, multiply, divide those depending upon your problem. This will be more clear when you will do some examples of Recursion in Java.

15 Recursion Programming Exercises for Java Programmers





15 Recursion Programming Exercises for Java Programmers

As I said the best way to learn Recursion in Java is to do examples, here are some of the programming exercises which can be solved using Recursion in Java. These recursion exercises are not too difficult and fun to solve, so try to solve them yourself before looking at answers and solutions. 

1. Write a program to calculate factorial using recursion in Java? (solution)

2. Write a program to Print Fibonacci Series in Java using Recursion? (solution)

3. Write a program to reverse String in Java using Recursion? (solution)

4. Write a countDown(int number) method in Java using Recursion which prints countdown till zero to console, like count(3) should print 3 2 1 0

hint:
public static void countDown(int number){
        if (number == 0) {
            System.out.println(number);
        } else {
            System.out.println(number);
            countDown(number - 1);
        }
 }

5. Write a Java program to reverse Linked List using Recursion? (solution)

6. Program to reverse a number using Recursion in Java?  (solution)

7. Write a Java program to print digitsToWords(int number) for example digitToWords(321) should print three two ones? (solution)

8. Write a Java program to calculate the power of a number like power(int number, int power) like power(2, 3) should return 8? (solution)

9. Write a program to calculate the sum of arithmetic series from 1 to N? (solution)

10. Write a program to calculate Greatest Common Divisor  GCD using Euclid's algorithm (solution)

11. Write a Java program to convert Decimal to binary using recursion? (solution)

12. How to print nodes of trees in the pre-order traversal using recursion? (solution)

13. How to code a post-order traversal algorithm using recursion? (solution)

14. How to implement inorder traversal of binary tree using recursion? (solution)

15. How to count the number of leaf nodes in a given binary tree? (solution)

16. How to calculate the sum of digits using recursion in Java? (solution)

17. How to find all permutations of a given String in Java? (solution)

18. How to implement a binary search algorithm using recursion? (solution)

19. How to implement bubble sort algorithms using recursion? (solution)


That's all on these 15 Java Recursion examples and exercises. Once you are comfortable with these easy recursive exercises you can move on to more complex recursive exercises like the famous Tower of Hanoi problem and several other dynamic programming-based problem which requires recursive solutions.  

I highly recommend you to try solving this problem by yourself, without taking help from the internet because that's the only way to learn recursion, your mind needs to be trained to understand the recursive solutions. Only see the solution if you can't solve it on your own. 


Other Important Resources for Coding and Programing Interviews:

Thanks a lot for reading this article so far. If you like these recursion-based coding problems and found this article useful in learning Recursion and recursion solutions, then please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you need a resource to understand recursion patterns, I highly recommend going through these best Recursion courses for programmers to learn how to identity recursive problems and how to break them down to solve using recursion. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.