Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

recursion-basics

Recursion basics

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Recursion basics: data structure and algorithm concepts with complexity analysis and interview-ready C++ examples.

Teams apply Recursion basics in every serious DSA project—skipping it leaves blind spots in analysis and reviews.

You will apply Recursion basics in contexts like: Interview loops, performance tuning, and foundational CS courses.

Compile and run C++17 snippets in the playground (`int main`, `std::cout`); after each run, state time and space complexity before moving on.

When you can explain the previous lesson's ideas in your own words.

Recursion solves a problem by calling itself on smaller subproblems. Every recursive function needs a base case and progress toward it—otherwise stack overflow.

Structure

  1. Base case — return known answer (n=0, empty tree)
  2. Recursive case — call on smaller input
  3. Combine results — often return value of subcall

Complexity

Factorial naive recursion depth n → O(n) stack space. Fibonacci naive recursion does overlapping work—O(2ⁿ) time without memoization.

Factorial example

#include 

long long fact(int n) {
    if (n <= 1) return 1;
    return n * fact(n - 1);
}

int main() {
    std::cout << "5! = " << fact(5) << "\n";
    return 0;
}

Important interview questions and answers

  1. Q: Missing base case?
    A: Infinite recursion until stack overflow.
  2. Q: When prefer iteration?
    A: Deep recursion or hot paths—loops avoid stack limits; tail recursion may optimize in some compilers.

Self-check

  1. What two parts must every recursion have?
  2. What is stack space for factorial(n)?

Tip: Every recursive call needs a base case that stops—say it aloud before coding.

Interview prep

Base case?

Stops recursion—without it, stack overflow.

Fibonacci naive?

O(2^n) time without memo—DP fixes overlap.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Base case why?
  • Fib naive cost?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump