Recursion Interview Questions

As we've studied before, recursion is the process of defining a problem in terms of itself, and is surprisingly handy when it comes to cracking tough programming interviews.

In this section, we'll go through problems involving recursion. Problems involving recursion often involve more focused visualization than those that don't, so don't be discouraged if you can't solve them right away. It's important to practice these problems, as they are a common interview topic.

Section Menu

How do I use this section?

1. CHALLENGE

Power of Three

Given an integer num, write a method to determine if it is a power of 3. The method will be called as follows: `js console.log(powerOfThree(9)); // true console.log(powerO...

2. CHALLENGE

Fibonacci Sequence

Implement a function that returns the Fibonnaci number for a given integer input. In a Fibonacci sequence, each number is the sum of the two preceding ones. The simplest is the series: 1, 1, 2, 3, 5, 8, etc. This is because: fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(2) = 1 fibonacci(3) = 1 + 1 = 2 fibonacci(4) = 1 + 2...

3. CHALLENGE

Max Product of Three Numbers

Given an unsorted array of integers, can you write a method maxProductOfThree(unsorted: array) to find the largest product from three of the numbers? For example, given the following array: [-1, 9, 22, 3, -15, -7] The largest product of three numbers is 2310. This results from -15 * -7 * 22. <img src="https://storage.goo...

4. CHALLENGE

Find Shortest Palindrome Possible

We have a string str like the following: `js const str = "bubble"; ` Find a way to convert it to a palindrome by inserting characters in front of it. Recall that a palindrome is defined as "a word, phrase, or sequence that reads the same backward as forward". <img src="https://storage.googleapis.com/algodailyrand...

5. CHALLENGE

Decimal To Binary

Today, we're going to do some math! There is only one prerequisite for answering this question, and it is knowing the definitions of binary and decimal. According to How to Convert Decimal to Binary and Binary to Decimal, binary is a numeric system that only uses two digits — 0 and...

6. CHALLENGE

Reverse a Linked List

You're sent a linked list of numbers, but it's been received in the opposite order to what you need. This has happened multiple times now, so you decide to write an algorithm to reverse the lists as they come in. The list you've received is as follows: 7 -> 2 -> 21 -> 6 -> 42 -> 10 Write an algorithm for a method `rever...

7. CHALLENGE

Sum of Perfect Squares

A perfect square is a number made by squaring a whole number. Some examples include 1, 4, 9, or 16, and so on -- because they are the squared results of 1, 2, 3, 4, etc. For example: ` 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 ... ` However, 15 is not a perfect square, because the square root of 15 is...

8. CHALLENGE

Flood Fill Paintbucket

Imagine the implementation for the paintbucket feature in Microsoft Paint, or how certain image filters work. You click on a specific pixel on a screen, and every similar-colored neighboring pixel would change to your selected color. Let's implement a simpler version of that feature. <img src="https://storage.googleapis.com/algo...

9. CHALLENGE

Classical N-Queen Problem

N-Queen Problem: The Chessboard Conundrum Imagine stepping into an interview room, and the interviewer asks you to solve the N-Queen problem. It's a modern take on the classic 8-Queen Puzzle, where you place queens on a chessboard in such a way that none can attack another. He...

10. CHALLENGE

Combination Sum

This challenge is all about a well-known problem in programming. You might have come across it in a job interview as part of a test, or even as part of a competition or exam. The problem is called Combination Sum and there are some variations in how it is presented, but this is the most common one: Given an array of distinct inte...

11. CHALLENGE

Power of a Number

Given a number x and an integer n, find the value of x raised to the power n ( xn ). image For example, consider the following two numbers, x = 2.1, n = 3 If the number 2.1 is multiplied by itself...