What is recursion?

In computer science, recursion is a method where the solution to a problem depends on solutions to smaller instances of the same problem. It involves a function calling itself directly or indirectly. Let's break it down with an example:

Basic Concept of Recursion:

  • Self-Referential Function: In recursion, a function solves a problem by calling itself with a smaller or simpler input.
  • Base Case: Every recursive function must have a base case, which is a condition that stops the recursion. Without a base case, the function would call itself indefinitely, leading to a stack overflow.
  • Recursive Case: This is where the function calls itself with a modified input.

Example: Factorial Calculation

The factorial of a number (n!) is the product of all positive integers up to that number (n). For instance, 5! = 5 × 4 × 3 × 2 × 1 = 120.

Here's how you could implement the factorial function using recursion:

def factorial(n): # Base case: factorial of 1 is 1 if n == 1: return 1 else: # Recursive case: n! = n * (n-1)! return n * factorial(n - 1) # Example usage print(factorial(5)) # Outputs: 120

In this example, factorial is a recursive function. It keeps calling itself with decreasing values of n until it reaches the base case (n == 1). At this point, it stops calling itself and begins to unwind and calculate the factorial.

Understanding Recursion:

  1. Stack Behavior: Each time a recursive function calls itself, a new frame is pushed onto the call stack with its own set of variables. Once a base case is reached, the stack begins to unwind, resolving each call.

  2. Visualization: Imagine a stack of books. Each recursive call adds a book to the stack. The base case is the last book, after which you stop stacking and start removing (or resolving) from the top.

  3. Usage in Algorithms: Recursion is commonly used in sorting algorithms (like quicksort, mergesort), tree and graph traversal (like in depth-first search), and dynamic programming.

Important Considerations:

  • Memory Usage: Recursive functions use more memory, as each function call consumes stack space.
  • Risk of Stack Overflow: Too many recursive calls can lead to a stack overflow error. This is why ensuring a proper base case is crucial.
  • Tail Recursion Optimization: In some languages, tail recursion (where the recursive call is the last operation in the function) can be optimized to avoid increasing the call stack size.

Recursion is a powerful and elegant tool in programming, especially useful in problems that can be broken down into similar subproblems. However, it's important to use it judiciously, keeping in mind its impact on memory and performance.

TAGS
Data Structures and Algorithms
Dynamic Programming Patterns
Coding Interview Questions
Coding Patterns
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

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
151. Reverse Words in a String - Detailed Explanation
How to reverse the word order of a string with split-reverse-join, and how to do the same work in place with two pointers.
994. Rotting Oranges - Detailed Explanation
How multi-source BFS spreads rot one layer per minute across the grid, and when the answer is -1.
What is a blind 75?
206. Reverse Linked List - Detailed Explanation
How three pointers reverse a singly linked list in a single pass, plus the recursive version and the pointer order that causes most bugs.
What is a group anagram?
Learn how to identify and group anagrams with this concise guide.
215. Kth Largest Element in an Array - Detailed Explanation
The three ways to pick the kth largest value, and why a min heap of size k costs less than sorting the whole array.
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.