Top 20 Recursion Practice Problems and Exercises for Programming Interviews
Tags: java, programming, coding, webdev
Published: 2022-08-06 13:57:36

---
title: Top 20 Recursion Practice Problems and Exercises for Programming Interviews
published: true
description: Preparing for coding interviews? These recursion exercises and practice problems are great to start with
tags: java, programming, coding, webdev
---
Hello guys, if 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 interview 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 its also very important and you cannot afford to leave it alone as its used to solve many [Dynamic Programming problems](https://javarevisited.blogspot.com/2021/03/top-dynamic-programming-problems-for-coding-interviews.html) like knapsack problem, Fibonacci etc. 

Solid knowledge of recursion is key to to do well on coding interview and that's where this article will help you. Earlier, I have shared frequently asked [programming interview questions](https://www.java67.com/2018/05/top-75-programming-interview-questions-answers.html), [books](http://www.java67.com/2017/06/10-books-to-prepare-technical-coding-job-interviews.html), and [courses](https://www.java67.com/2019/07/top-10-online-courses-to-learn-data-structure-and-algorithms-in-java.html) 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](https://medium.com/javarevisited/7-best-courses-to-learn-recursion-for-programming-and-coding-interviews-1f8b0bcfd44) 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](https://javarevisited.blogspot.com/2021/09/top-10-recursion-interview-questions.html).

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](https://medium.com/javarevisited/top-20-linked-list-coding-problems-from-technical-interviews-90b64d2df093) and [binary tree-based problems](https://medium.com/javarevisited/20-binary-tree-algorithms-problems-from-coding-interviews-c5e5a384df30) 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](https://medium.com/javarevisited/7-best-courses-to-learn-recursion-for-programming-and-coding-interviews-1f8b0bcfd44) 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](https://javarevisited.blogspot.com/2015/08/how-to-calculate-large-factorials-using-BigInteger-Java-Example.html) than factorial(1) = 1 is a base case, for [Fibonacci numbers](https://www.java67.com/2019/03/nth-fibonacci-number-in-java-coding.html) 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](https://1.bp.blogspot.com/--DioXEZWa-g/YOaBRH6vkNI/AAAAAAAAobA/7RElM_gJ8iEpedDBFrSBtcoKv6fdx-kbwCLcBGAsYHQ/w384-h242/fibonacci-series%2Bin%2BJava.gif)](https://javarevisited.blogspot.com/2011/06/top-programming-interview-questions.html)

------


## How to 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](https://javarevisited.blogspot.com/2015/08/how-to-calculate-large-factorials-using-BigInteger-Java-Example.html), 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](https://www.java67.com/2018/04/21-string-programming-and-coding-interview-questions-answers.html), [Array-based problems](https://javarevisited.blogspot.com/2015/06/top-20-array-interview-questions-and-answers.html#axzz5dCg1cNYo), [linked list algorithms](https://javarevisited.blogspot.com/2017/07/top-10-linked-list-coding-questions-and.html), [binary tree algorithms](https://www.java67.com/2020/02/top-40-binary-tree-interview-questions.html), and [dynamic programming-based coding problems](https://javarevisited.blogspot.com/2021/03/top-dynamic-programming-problems-for-coding-interviews.html) 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 solve [Recursive Practice Questions in Java](https://javarevisited.blogspot.com/2012/12/recursion-in-java-with-example-programming.html).

[![15 Recursion Programming Exercises for Java Programmers](https://1.bp.blogspot.com/--x3BX9QfD40/YOZ98svAHXI/AAAAAAAAoa0/FrRWY7kuKEEuTqXSIYePJwJMQj-urd4_QCLcBGAsYHQ/w411-h328/Recursion%2Bfor%2BCoding%2BInterview%2BEducative%2BCourse%2B-%2Bbest%2Bin%2BJava.png)](https://javarevisited.blogspot.com/2021/11/top-5-courses-to-learn-recursion-for.html)

If you like video, you can also see this Youtube Video which gives 5 easy steps to solve Recursion problem

{% youtube https://www.youtube.com/watch?v=ngCos392W4w %}


###Recursion Practiced Problems and Exercises Coding Interviews
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](http://www.java67.com/2015/09/factorial-in-java-using-recursion-and-loop.html))

2\. Write a program to Print Fibonacci Series in Java using Recursion? ([solution](https://www.java67.com/2016/05/fibonacci-series-in-java-using-recursion.html))

3\. Write a program to reverse String in Java using Recursion? ([solution](https://www.java67.com/2012/12/how-to-reverse-string-in-java-stringbuffer-stringbuilder.html))

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:
```java
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](https://javarevisited.blogspot.com/2017/03/how-to-reverse-linked-list-in-java-using-iteration-and-recursion.html#axzz6e8hmwujv))

6\. Program to reverse a number using Recursion in Java?  ([solution](https://javarevisited.blogspot.com/2012/04/java-program-to-reverse-number-example.html))

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](https://www.java67.com/2018/05/how-to-implement-power-function-in-java.html))

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

10\. Write a program to calculate Greatest Common Division  GCD using Euclid's algorithm ([solution](http://www.java67.com/2012/08/java-program-to-find-gcd-of-two-numbers.html))

11\. Write a Java program to convert Decimal to binary using recursion? ([solution](http://www.java67.com/2014/03/decimal-to-binary-conversion-in-java.html))

12\. How to print nodes of trees in the pre-order traversal using recursion? ([solution](https://javarevisited.blogspot.com/2016/07/binary-tree-preorder-traversal-in-java-using-recursion-iteration-example.html#axzz6dXsEfLvJ))

13\. How to code a post-order traversal algorithm using recursion? ([solution](https://javarevisited.blogspot.com/2016/10/post-order-binary-tree-traversal-in-java-iteration-recursion.html#axzz5ghwj3kuU))

14\. How to implement inorder traversal of binary tree using recursion? ([solution](https://javarevisited.blogspot.com/2016/08/inorder-traversal-of-binary-tree-in-java-recursion-iteration-example.html))

15\. How to count the number of leaf nodes in a given binary tree? ([solution](https://javarevisited.blogspot.com/2016/12/how-to-count-number-of-leaf-nodes-in-java-recursive-iterative-algorithm.html#axzz6e8hmwujv))

16\. How to calculate the sum of digits using recursion in Java? ([solution](http://www.java67.com/2014/06/write-program-to-find-sum-of-digits-in-java.html))

17\. How to find all permutations of a given String in Java? ([solution](https://javarevisited.blogspot.com/2015/08/how-to-find-all-permutations-of-string-java-example.html#axzz5N6yGHupv))

18\. How to implement a binary search algorithm using recursion? ([solution](http://www.java67.com/2016/05/java-program-to-perform-binary-search-without-recursion.html))

19\. How to implement bubble sort algorithms using recursion? ([solution](https://www.java67.com/2012/12/bubble-sort-in-java-program-to-sort-integer-array-example.html))


20. How to solve Tower of Hanoi problem using recursion?

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:

-   [Top 5 Free Data Structure and Algorithm Courses](https://javarevisited.blogspot.com/2018/01/top-5-free-data-structure-and-algorithm-courses-java--c-programmers.html)
-   [10 Courses to Prepare for Programming Job Interviews](http://javarevisited.blogspot.sg/2018/02/10-courses-to-prepare-for-programming-job-interviews.html)
-   [20+ String Algorithms Interview Questions](https://hackernoon.com/20-string-coding-interview-questions-for-programmers-6b6735b6d31c)
-   [Review these Java Interview Questions for Programmers](https://www.freecodecamp.org/news/review-these-50-questions-to-crack-your-java-programming-interview-69d03d746b7f/)
-   [Top 5 Data Structure and Algorithm Books for Java Developers](http://javarevisited.blogspot.sg/2016/05/5-free-data-structure-and-algorithm-books-in-java.html#axzz4uXETWjmV)
-   [20+ array-based Problems for interviews](https://medium.com/javarevisited/20-array-coding-problems-and-questions-from-programming-interviews-869b475b9121)
-   [25 Software Design Interview Questions for Programmers](https://medium.com/javarevisited/25-software-design-interview-questions-to-crack-any-programming-and-technical-interviews-4b8237942db0)
-   [Top 30 Object-Oriented Programming Questions](http://www.java67.com/2015/12/top-30-oops-concept-interview-questions-answers-java.html)
-   [10 Algorithms Courses Junior Developer should join](http://www.java67.com/2019/02/top-10-free-algorithms-and-data.html)
-   [10 Algorithm Books Every Programmer Should Read](http://www.java67.com/2015/09/top-10-algorithm-books-every-programmer-read-learn.html)
-   [7 Best Courses to learn Data Structure and Algorithms](https://medium.com/javarevisited/7-best-courses-to-learn-data-structure-and-algorithms-d5379ae2588)
-   [Top 5 Courses to learn Dynamic Programming for Interviews](https://javarevisited.blogspot.com/2019/12/top-5-courses-to-learn-dynamic-programming-for-interivews.html)


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](https://www.java67.com/2021/07/6 Best Golang Programming Courses for Beginners to Learn in 2022 [UPDATED] - Best of Lot https://buff.ly/2TrpgBA)** to learn how to identity recursive problems and how to break them down to solve using recursionoklv>