Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
FOUNDATIONS

Recursion

Solve a problem by solving a smaller copy of it, until you hit a base case.

Intermediate8 problems3 interactive subtopicsO(n) time · O(n) stack
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Call factorial(4)
n = 4
CALL STACK - TOP AT THE TOP
factorial(4)
  • current
  • not reached yet
  • result
  • removed / popped
Space Play←→ StepR Replay
1 / 5
PYTHONfactorial(4)
1def factorial(n):
2 if n <= 1:
3 return 1
▶4 return n * factorial(n - 1)
STEP 1 OF 5Setup

Call factorial(4)

4 isn’t a base case, so it needs factorial(3) first and waits.

VARIABLES

n
4
call stack
factorial(4)
returned
-
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A recursive function calls itself on a smaller input. Each call waits on the call stack until the smaller one returns.

Why it matters

Trees, nested structures and divide-and-conquer problems are naturally defined in terms of smaller versions of themselves.

Think of it like this

Russian nesting dolls: open each one to find a smaller one, until the last doll can’t be opened.

Use it when

  • The structure is recursive - trees, nested lists, expressions
  • Divide and conquer: split, solve the halves, combine
  • Exploring choices, as in backtracking

Avoid it when

  • Very deep inputs in Python - the default recursion limit is about 1000 frames
  • Overlapping subproblems without memoization - the work can grow exponentially

Complexity cheatsheet

OperationTimeSpace
Linear recursion, depth nO(n)O(n) stack
Naive binary recursion (fib)O(2ⁿ)O(n) stack
Divide and conquer (merge sort)O(n log n)O(n)

Often confused with: iteration with an explicit stack - the same logic, without the recursion limit.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

treenestedall combinationsdividedepth
  • If the answer for n uses the answer for n − 1, recursion fits
  • A linked list of 10⁵ nodes is too deep for plain recursion in Python
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • A missing or unreachable base case - infinite recursion
  • Not shrinking the input on each call
  • Recomputing the same subproblem many times instead of memoizing

Edge cases to test

  • n = 0 or n = 1 - the base cases themselves
  • Negative input - decide how to reject it
  • Large n - hits the recursion limit
Interview tip: Say the base case and the recurrence out loud before coding - interviewers look for both.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Naive recursive fib(n) calls fib(n − 1) and fib(n − 2). Why is it slow?

06

Practice

Apply what you just watched on real interview problems.

Generate Backtracking problem in AI DSA Practice →
  • BeginnerFibonacci NumberLeetCode 509 ↗ (opens in a new tab)
  • IntermediatePow(x, n)LeetCode 50 ↗ (opens in a new tab)
  • AdvancedRegular Expression MatchingLeetCode 10 ↗ (opens in a new tab)
StringsSorting