DSA Tracker

Blog

Guide

Recursion for Beginners: Base Cases, Choices and the Call Stack

Master recursion for coding interviews by starting with the base case, trusting smaller calls, and tracing the call stack step by step.

Riya Kushwaha, DSA Tracker team3 min read
On this page

Recursion feels impossible during data structures and algorithms prep because you try to hold every single function call in your head at once. The real secret is that you must stop trying to trace the middle layers and instead write the base case first, assume the smaller call works, and draw the stack explicitly on paper.

The Base Case Comes First

Write the termination condition before you write the function logic. If you start by writing how the problem splits, you will forget when it stops and hit a stack overflow error.

Consider writing a function to compute factorial(n). If n is 0, return 1. That line stops the infinite loop. If you write factorial(n - 1) before handling n == 0, your program will run until memory runs out on inputs like n = 5.

Trust the Smaller Call

When you write factorial(n), assume that factorial(n - 1) already works correctly and returns the right answer. Do not mentally step into the recursive call for n - 4 while writing the code for n.

If your base case is correct and your reduction step brings n closer to 0 by 1 each time, the math holds. You combine the current step with the trusted result of the smaller call, returning n * factorial(n - 1).

Trace the Stack by Hand

Take a small input like reverseString("abc") and draw three boxes stacked vertically on your notebook. The top box is reverseString("abc"), which calls reverseString("bc"), which calls reverseString("c"), which hits the base case and returns "c".

Watch the return values bubble back up the stack. The base case returns "c", the middle frame appends "b" to get "bc", and the bottom frame appends "a" to get "cb". If you skip drawing this stack for your first twenty recursion problems, you are just guessing.

Subsets and Choices

Problems like generating all subsets of [1, 2] require a choice tree rather than a single linear chain. At index 0, you either include 1 or exclude 1, then recurse on index 1.

Your function passes a current subset list and an index integer down the tree. When the index reaches the length of the array, you print or store the current subset and return.

The Screenshot Rule

Write these four lines on a sticky note and put it on your monitor:

  1. Write the base case and return value before anything else.
  2. Assume the function works for n - 1 or arr + 1.
  3. Draw three stack frames on paper for every bug.
  4. Pass state changes as arguments instead of mutating global variables.

Factorial Trace Example

Here is how factorial(3) executes in memory. factorial(3) calls 3 * factorial(2). factorial(2) calls 2 * factorial(1). factorial(1) calls 1 * factorial(0). factorial(0) hits the base case and returns 1. Then factorial(1) evaluates 1 * 1 and returns 1. Then factorial(2) evaluates 2 * 1 and returns 2. Then factorial(3) evaluates 3 * 2 and returns 6.

Open DSA Tracker, pick the problem subsets, and trace the recursion tree on paper before writing a single line of code.

Keep going with practice problems and the online compiler.

Further reading: Stacks on Wikipedia.

Share

XLinkedInWhatsApp

Frequently asked questions

Why does my recursive function return undefined or zero at the end?

You probably forgot to use the return keyword in front of the recursive function call, so the value computed in deeper stack frames is lost when control returns upward. |

How do I know if I need recursion or iteration?

Use recursion when the input has a tree or nested structure like subsets and graph nodes where the depth is unknown, and use loops for flat arrays. |

What is a stack overflow error in recursion?

It happens when your base case is missing or unreachable, causing the computer to exhaust all allocated memory for new function call frames. |

Practice what you just read

Open one in the editor and submit it. Your code is checked against hidden tests in C++, Java, Python and JavaScript. No account needed.

Keep reading