Skip to content

🟣 Recursion Algorithm interview questions and answers to help you prepare for your next data structures and algorithms interview in 2024.

Notifications You must be signed in to change notification settings

Devinterview-io/recursion-algorithm-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

🖲 Recursion Data Structures interview questions for developers in 2021

In computer science, recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. Follow along and check our list of essential Recursion interview questions and answers that will trend on data structures interviews in 2021.



You can also find all answers here 👉🏼 https://devinterview.io/data/recursion-interview-questions


🔹 1. How Dynamic Programming is different from Recursion and Memoization?

Answer:

  • Memoization is when you store previous results of a function call (a real function always returns the same thing, given the same inputs). It doesn't make a difference for algorithmic complexity before the results are stored.
  • Recursion is the method of a function calling itself, usually with a smaller dataset. Since most recursive functions can be converted to similar iterative functions, this doesn't make a difference for algorithmic complexity either.
  • Dynamic programming is the process of solving easier-to-solve sub-problems and building up the answer from that. Most DP algorithms will be in the running times between a Greedy algorithm (if one exists) and an exponential (enumerate all possibilities and find the best one) algorithm.
    • DP algorithms could be implemented with recursion, but they don't have to be.
    • DP algorithms can't be sped up by memoization, since each sub-problem is only ever solved (or the "solve" function called) once.
Source: stackoverflow.com   


🔹 2. What is a good example of Recursion (other than generating a Fibonacci sequence)?

Answer:

There are some:

  • The binary tree search
  • Check for a palyndrome
  • Finding factorial
  • Traversing the folder hierarchy of a directory tree as part of a file system
  • Towers of Hanoi
  • Merge sort
  • Catalan numbers
Source: stackoverflow.com   


🔹 3. What is the difference between Backtracking and Recursion?

Answer:

  • Recursion describes the calling of the same function that you are in. The typical example of a recursive function is the factorial. You always need a condition that makes recursion stop (base case).
  • Backtracking is when the algorithm makes an opportunistic decision*, which may come up to be wrong. If the decision was wrong then the backtracking algorithm restores the state before the decision. It builds candidates for the solution and abandons those which cannot fulfill the conditions. A typical example for a task to solve would be the Eight Queens Puzzle. Backtracking is also commonly used within Neuronal Networks. Many times backtracking is not implemented recursively. If backtracking uses recursion its called Recursive Backtracking

P.S. * Opportunistic decision making refers to a process where a person or group assesses alternative actions made possible by the favorable convergence of immediate circumstances recognized without reference to any general plan.

Source: www.quora.com   


🔹 4. Can you convert this Recursion function into a loop?

Answer:

Problem

Can you convert this recursion function into a loop?

A(x) {
  if x<0 return 0;
  return something(x) + A(x-1)
}

Any recursive function can be made to iterate (into a loop) but you need to use a stack yourself to keep the state.

A(x) {
  temp = 0;
  for i in 0..x {
    temp = temp + something(i);
  }
  return temp;
}
Source: stackoverflow.com   


🔹 5. Explain what is DFS (Depth First Search) algorithm for a Graph and how does it work?

Answer:

Depth First Traversal or Depth First Search is a edge based recursive algorithm for traversing (visiting) all the vertices of a graph or tree data structure using a stack. The purpose of the algorithm is to mark each vertex as visited while avoiding cycles. DFS traverse/visit each vertex exactly once and each edge is inspected exactly twice. DFS is a genuinely recursive algorithm that uses stack for backtracking purposes, not for storing the vertex discovery "front" (as is the case in BFS).

The DFS algorithm works as follows:

  • Start by putting any one of the graph's vertices on top of a stack.
  • Take the top item of the stack and add it to the visited list.
  • Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the top of the stack.
  • Keep repeating steps 2 and 3 until the stack is empty.

DFS example step-by-step:

  • Considering A as the starting vertex which is explored and stored in the stack.
  • B successor vertex of A is stored in the stack.
  • Vertex B have two successors E and F, among them alphabetically E is explored first and stored in the stack.
  • The successor of vertex E, i.e., G is stored in the stack.
  • Vertex G have two connected vertices, and both are already visited, so G is popped out from the stack.
  • Similarly, E s also removed.
  • Now, vertex B is at the top of the stack, its another node(vertex) F is explored and stored in the stack.
  • Vertex F has two successor C and D, between them C is traversed first and stored in the stack.
  • Vertex C only have one predecessor which is already visited, so it is removed from the stack.
  • Now vertex D connected to F is visited and stored in the stack.
  • As vertex D doesn’t have any unvisited nodes, therefore D is removed.
  • Similarly, F, B and A are also popped.
  • The generated output is – A, B, E, G, F, C, D.

Source: techdifferences.com   


🔹 6. Convert a Binary Tree to a Doubly Linked List

Answer:

This can be achieved by traversing the tree in the in-order manner that is, left the child -> root ->right node.

In an in-order traversal, first the left sub-tree is traversed, then the root is visited, and finally the right sub-tree is traversed.

One simple way of solving this problem is to start with an empty doubly linked list. While doing the in-order traversal of the binary tree, keep inserting each element output into the doubly linked list. But, if we look at the question carefully, the interviewer wants us to convert the binary tree to a doubly linked list in-place i.e. we should not create new nodes for the doubly linked list.

This problem can be solved recursively using a divide and conquer approach. Below is the algorithm specified.

  • Start with the root node and solve left and right sub-trees recursively
  • At each step, once left and right sub-trees have been processed:
    • fuse output of left subtree with root to make the intermediate result
    • fuse intermediate result (built in the previous step) with output from the right sub-tree to make the final result of the current recursive call
Complexity Analysis
Time:
Constant
O(1)
Dbl. Logarithmic
O(log log n)
Logarithmic
O(log n)
Square Root
O(√n)
Linear
O(n)
Linearithmic
O(n log n)
Quadratic
O(n2)
Exponential
O(2n)
Factorial
O(n!)
Space:
Constant
O(1)
Dbl. Logarithmic
O(log log n)
Logarithmic
O(log n)
Square Root
O(√n)
Linear
O(n)
Linearithmic
O(n log n)
Quadratic
O(n2)
Exponential
O(2n)
Factorial
O(n!)

Time: O(n)

Space: O(n)

Recursive solution has O(h) memory complexity as it will consume memory on the stack up to the height of binary tree h. It will be O(log n) for balanced tree and in worst case can be O(n).

Implementation
Java Java
Python PY
public static void BinaryTreeToDLL(Node root) {
    if (root == null)
        return;
    BinaryTreeToDLL(root.left);
    if (prev == null) { // first node in list
        head = root;
    } else {
        prev.right = root;
        root.left = prev;
    }
    prev = root;
    BinaryTreeToDLL(root.right);
    if (prev.right == null) { // last node in list
        head.left = prev;
        prev.right = head;
    }
}
Source: www.educative.io   


🔹 7. Is it always possible to write a non-recursive form for every Recursive function?

Answer:

When you use a function recursively, the compiler takes care of stack management for you, which is what makes recursion possible. Anything you can do recursively, you can do by managing a stack yourself (for indirect recursion, you just have to make sure your different functions share that stack).

A simple formal proof is to show that both µ recursion (general recursive function) and a non-recursive calculus such as GOTO are both Turing complete. Since all Turing complete calculi are strictly equivalent in their expressive power, all recursive functions can be implemented by the non-recursive Turing-complete calculus.

Source: stackoverflow.com   


🔹 8. How to use Memoization for N-th Fibonacci number?

Answer:

Problem

Why? Any problems you may face with that solution?

Yes. It's called Memoization. Memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls.

Let’s look at the diagram that will help you understand what’s going on here with the rest of our code. Function fib is called with argument 5. Can you see that we calculate the fib(2) results 3(!) times?

Basically, if we just store the value of each index in a hash, we will avoid the computational time of that value for the next N times. This change will increase the space complexity of our new algorithm to O(n) but will dramatically decrease the time complexity to 2N which will resolve to linear time since 2 is a constant O(n).

There’s just one problem: With an infinite series, the memo array will have unbounded growth. Eventually, you’re going to run into heap size limits, and that will crash the JS engine. No worries though. With Fibonacci, you’ll run into the maximum exact JavaScript integer size first, which is 9007199254740991. That’s over 9 quadrillion, which is a big number, but Fibonacci isn’t impressed. Fibonacci grows fast. You’ll burst that barrier after generating only 79 numbers.

Complexity Analysis
Time:
Constant
O(1)
Dbl. Logarithmic
O(log log n)
Logarithmic
O(log n)
Square Root
O(√n)
Linear
O(n)
Linearithmic
O(n log n)
Quadratic
O(n2)
Exponential
O(2n)
Factorial
O(n!)
Space:
Constant
O(1)
Dbl. Logarithmic
O(log log n)
Logarithmic
O(log n)
Square Root
O(√n)
Linear
O(n)
Linearithmic
O(n log n)
Quadratic
O(n2)
Exponential
O(2n)
Factorial
O(n!)

Time: O(n)

Space: O(n)

Implementation
JavaScript JS
Java Java
function fibonacci(num, memo) {
  memo = memo || {};

if (memo[num]) return memo[num]; if (num <= 1) return 1;

return memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo); }

Source: medium.com   


🔹 9. How to recursively reverse a Linked List?

Answer:

Start from the bottom up by asking and answering tiny questions:

  • What is the reverse of null (the empty list)? null.
  • What is the reverse of a one element list? the element.
  • What is the reverse of an n element list? the reverse of the rest of the list followed by the first element.

P.S.

How to understand that part?

head.next.next = head;
head.next = null;

Think about the origin link list:

1->2->3->4->5

Now assume that the last node has been reversed. Just like this:

1->2->3->4<-5

And this time you are at the node 3 , you want to change 3->4 to 3<-4 , means let 3->next->next = 3 (as 3->next is 4 and 4->next = 3 is to reverse it)

Complexity Analysis
Time:
Constant
O(1)
Dbl. Logarithmic
O(log log n)
Logarithmic
O(log n)
Square Root
O(√n)
Linear
O(n)
Linearithmic
O(n log n)
Quadratic
O(n2)
Exponential
O(2n)
Factorial
O(n!)
Space:
Constant
O(1)
Dbl. Logarithmic
O(log log n)
Logarithmic
O(log n)
Square Root
O(√n)
Linear
O(n)
Linearithmic
O(n log n)
Quadratic
O(n2)
Exponential
O(2n)
Factorial
O(n!)

Time: O(n)

Space: O(n)

Assume that n is the list's length, the time complexity is O(n). The extra space comes from implicit stack space due to recursion. The recursion could go up to n levels deep then space complexity is O(n).

Implementation
JavaScript JS
Java Java
Python PY
var reverseList = function(head) {
if (!head) {
return head;
}
let pre = head.next;
head.next = null;
return fun(head, pre);

<span class="token cVar">function</span> <span class="token cMod">fun</span><span class="token cBase">(</span><span class="token parameter">cur<span class="token cBase">,</span> pre</span><span class="token cBase">)</span> <span class="token cBase">{</span>
    <span class="token cVar">if</span> <span class="token cBase">(</span>pre <span class="token cBase">==</span> <span class="token cVar">null</span><span class="token cBase">)</span> <span class="token cBase">{</span>
        <span class="token cVar">return</span> cur<span class="token cBase">;</span>
    <span class="token cBase">}</span>
    <span class="token cVar">let</span> tmp <span class="token cBase">=</span> pre<span class="token cBase">.</span>next<span class="token cBase">;</span>
    pre<span class="token cBase">.</span>next <span class="token cBase">=</span> cur<span class="token cBase">;</span>
    <span class="token cVar">return</span> <span class="token cMod">fun</span><span class="token cBase">(</span>pre<span class="token cBase">,</span> tmp<span class="token cBase">)</span><span class="token cBase">;</span>
<span class="token cBase">}</span>

}

Source: stackoverflow.com   



You can also find more data structures interview questions here 👉🏼 https://devinterview.io/data/