Coding interview questions requiring recursion may be some of the hardest questions out there. But they are also some of the most important.
Interviewers love to ask recursion interview questions because they truly push interviewees to the limit, and allow interviewers to differentiate between those candidates who are just good and those who are exceptional.
Not only that, but recursive techniques tend to bleed into almost every other category of question.
Strings? Edit Distance or Longest Common Substring.
Trees and Graphs? Depth First Search.
Dynamic Programming? Recursion is the first step of the FAST Method.
Suffice to say, it is absolutely essential that you be prepared to solve recursion interview questions in your interview. It is almost guaranteed that you will see at least one or two recursive problems at any given onsite interview.
In this post, I will walk you through 12 of the most common recursion interview questions that you might see in your coding interview. If you understand these problems and the underlying strategies, you will be well on your way to nailing any recursive problem.
As you go through each of these problems, I would highly encourage you to attempt the problem on your own before watching the solution. Ideally, you should simply be using the solutions as a reference.
Protip: If you’re still new to recursion, check out our Ultimate Guide to Recursion first
Check out the problems:
1. Making Change
Given an input amount of change x
, write a function to determine the minimum number of coins required to make that amount of change.
eg. (using American coins)
1 2 3 4 |
change(1) = 1 change(3) = 3 change(7) = 3 change(32) = 4 |
2. Binary Search Tree Verification
Given a binary tree, write a function to test if the tree is a binary search tree.
eg.
Valid:
Invalid:
3. Find All Permutations of an Input
Write a function that returns all permutations of a given list.
eg.
1 2 3 4 5 6 7 |
permutations({1, 2, 3}) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] |
4. Balanced Binary Tree
Given a binary tree, write a function to determine whether the tree is balanced.
eg.
5. Print a Linked List in Reverse Order
Given a linked list, write a function that prints the nodes of the list in reverse order.
eg.
1 2 3 4 |
printReversedList(1 -> 2 -> 3) 3 2 1 |
6. Longest Consecutive Branch in a Tree
Given a tree, write a function to find the length of the longest branch of nodes in increasing consecutive order.
eg.
7. Tree to Doubly Linked List
Given a tree, write a function to convert it into a circular doubly linked list from left to right by only modifying the existing pointers.
eg.
<- 4 <-> 2 <-> 5 <-> 1 <-> 6 <-> 3 <-> 7 ->
8. Nth Fibonacci Number
Given an integer n
, write a function to compute the nth Fibonacci number.
eg.
1 2 3 |
fibonacci(1) = 1 fibonacci(5) = 5 fibonacci(10) = 55 |
9. Reverse a Stack
Given a stack, reverse the items without creating any additional data structures.
eg.
reverse(1->2->3) = 3->2->1
10. Compute the Sum
Given two integers, write a function to sum the numbers without using any arithmetic operators.
11. Find the Lowest Common Ancestor
Given two nodes in a binary tree, write a function to find the lowest common ancestor.
eg.
1 2 |
lcs(4, 3) = 1 lcs(6, 7) = 3 |
12. Select a Random Binary Tree Node
Implement a binary tree with a method getRandomNode()
that returns a random node.
eg.
1 2 3 |
getRandomNode() = 5 getRandomNode() = 8 getRandomNode() = 1 |
Have you seen any of these problems in an interview before? Comment below and let me know!