Give simple tail-recursive function in C++. What other kinds of recursion are there besides tail recursion?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Tail-Recursive Function in C++

A tail-recursive function is a recursive function where the recursive call is the last operation in the function, with no further computation required after the call returns. This enables compilers to optimize the recursion by reusing the same stack frame, avoiding stack overflow for deep recursion.

Example: Tail-Recursive Factorial in C++

#include <iostream> // Tail-recursive factorial function int factorialHelper(int n, int accumulator) { if (n == 0 || n == 1) { return accumulator; } return factorialHelper(n - 1, accumulator * n); // Tail call } int factorial(int n) { return factorialHelper(n, 1); } int main() { std::cout << "Factorial of 5 is: " << factorial(5) << std::endl; // Output: 120 return 0; }

Explanation:

  • factorialHelper carries an additional parameter (accumulator) that holds the result of the calculations.
  • The recursive call to factorialHelper is the last operation in the function, making it tail-recursive.

Types of Recursion Besides Tail Recursion

  1. Head Recursion

    • The recursive call is made at the beginning of the function, and computation happens after the call returns.
    • Example:
      void printReverse(int n) { if (n == 0) return; printReverse(n - 1); // Recursive call first (head recursion) std::cout << n << " "; }
  2. Linear Recursion

    • Each function calls itself at most once, forming a straight chain of calls.
    • Example:
      int sum(int n) { if (n == 0) return 0; return n + sum(n - 1); // Single recursive call }
  3. Tree Recursion

    • The function makes multiple recursive calls at each step, creating a tree-like structure of calls.
    • Example:
      int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); // Multiple recursive calls }
  4. Indirect Recursion

    • A function calls another function that eventually calls the first function, creating a cycle of calls.
    • Example:
      void funcA(int n); void funcB(int n) { if (n <= 0) return; funcA(n - 1); // Calls funcA } void funcA(int n) { if (n <= 0) return; funcB(n - 1); // Calls funcB }
  5. Mutual Recursion

    • A specific case of indirect recursion where two or more functions call each other directly.
    • Example:
      bool isEven(int n); bool isOdd(int n) { if (n == 0) return false; return isEven(n - 1); // Calls isEven } bool isEven(int n) { if (n == 0) return true; return isOdd(n - 1); // Calls isOdd }
  6. Nested Recursion

    • A function calls itself within its own recursive call.
    • Example:
      int nestedRecursion(int n) { if (n > 100) return n - 10; return nestedRecursion(nestedRecursion(n + 11)); // Calls itself within its call }

Summary of Recursion Types

Recursion TypeDescriptionExample
Tail RecursionRecursive call is the last operation.Factorial with accumulator
Head RecursionRecursive call occurs first, computation happens after.Printing numbers in reverse
Linear RecursionEach function calls itself once, forming a chain.Sum of integers
Tree RecursionFunction makes multiple recursive calls.Fibonacci
Indirect RecursionFunction calls another function that eventually calls it back.funcA and funcB example
Mutual RecursionFunctions call each other directly.isEven and isOdd example
Nested RecursionRecursive calls are nested within other recursive calls.Nested recursion with conditions

By understanding these types, you can choose the appropriate recursive strategy for solving problems effectively. Tail recursion is particularly useful for avoiding stack overflow when recursion depth is significant, provided the compiler supports tail call optimization.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Practice drills for explaining code choices to non-technical stakeholders
How to check if a number is a power of 2?
Should I look at LinkedIn before interview?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.