Join us

20 Recursion based Practice Problems and Exercises for Beginners

recursion practice problem and coding exercises

Hello guys, if you struggle to solve coding problems using recursion or have difficulty in understanding the Recusion then you are not alone. Many people struggle to understand Recursion and the only way to overcome that is solve as many Recursion coding exercises, homework, and Recursive problems as possible. This will train your brain to understand Recursion, base case, and how recursive algorithm works in genera.

you are preparing for coding interviews or trying to learn recursion but struggling and looking for recursion practice problems, recursive coding problems, or simple recursion exercises for interviews then you have come to the right place. Recursion is a simple yet difficult topic for many programmers as not everyone can understand recursion easily but it's also very important and you cannot afford to leave it alone as it's used to solve many Dynamic Programming problems like knapsack problems, Fibonacci, etc.

Solid knowledge of recursion is key to doing well in the coding interviews and that's where this article will help you. Earlier, I have shared frequently asked programming interview questions, books, and courses and here, I will share 15 Recursion practice problems and exercises you can do to learn recursion from scratch.

These are common programming questions and practice 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, few 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.

Since I am a Java programmer, you will Recursion based practice problems which are solved in the Java programming language but you are free to solve any programming language like C or Python or even JavaScript.

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 identify 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 the 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.

By the way, if you like to learn from interactive courses then you can also join Recursion for Coding Interviews in Java a great interactive course from Educative to learn Recursion better, I really loved it as it also forms the basis for Dynamic Programming which they have explained in their  Grokking Dynamic Programming Patterns for Coding Interview course.

How do find if a Problem 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, and if you want to learn more, you can always join one of these best Recursion courses for beginners where I have shared both beginner and advanced level Recursion courses for coding interviews and learning Recursion from scratch.

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 toward 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, and divide those depending upon your problem. This will be more clear when you will solve Recursive Practice Questions in Java.

And, if you want to learn Recursion from scratch then Recursion for Coding Interviews in Java course on Educative is a great resource to start with, I really loved it as it also forms the basis for Dynamic Programming which they have explained in their  Grokking Dynamic Programming Patterns for Coding Interview course.

20 Recursion-Based Coding Problems and Exercises for 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


public static void countDown(int number){
if (number == 0) {
} else {
countDown(number - 1);

  1. How to reverse Linked List using Recursion. (solution)
  2. How to print digitsToWords(int number) for example digitToWords(321) should print three or two ones? (solution)
  3. Program to reverse a number using Recursion in Java?  (solution)
  4. How to calculate the sum of arithmetic series from 1 to N? (solution)
  5. How to calculate the power of a number like power(int number, int power) like power(2, 3) should return 8? (solution)
  6. How to calculate Greatest Common Division  GCD using Euclid's algorithm (solution)
  7. How to print nodes of trees in the pre-order traversal using recursion? (solution)
  8. Write a Java program to convert Decimal to binary using recursion. (solution)
  9. How to code a post-order traversal algorithm using recursion? (solution)
  10. How to implement inorder traversal of binary tree using recursion? (solution)
  11. How to count the number of leaf nodes in a given binary tree? (solution)
  12. How to implement a binary search algorithm using recursion? (solution)
  13. How to find all permutations of a given String in Java? (solution)
  14. How to calculate the sum of digits using recursion in Java? (solution)
  15. How to solve the Tower of Hanoi problem using recursion?
  16. How to implement bubble sort algorithms using recursion? (solution)

That's all on these 20 Recursion Practice Problems 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 Coding and Programing Interview Resources You may like:

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 identify recursive problems and how to break them down to solve using recursion.

Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!


Javin Paul

Blogger, Programmer, Developer

Blogger - Creator - Instructor - Website - Follow me for Java resources
User Popularity



Total Hits