{
  "id": "recursion-backtracking/recursion",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/recursion-backtracking/recursion",
  "topic": {
    "slug": "recursion-backtracking",
    "title": "Recursion & Backtracking"
  },
  "title": "Recursion & the Call Stack",
  "tagline": "A function that calls itself — and the pile of calls that piles up.",
  "vizKind": "callstack",
  "complexity": {
    "time": {
      "best": "O(n)",
      "average": "O(n)",
      "worst": "O(n)"
    },
    "space": "O(n)",
    "growth": "linear",
    "note": "n calls, so n multiplications: O(n) time. But it also uses O(n) *space* — every waiting call takes up room on the stack. A loop doing the same job uses O(1) space. Recursion is never free."
  },
  "code": {
    "javascript": "function factorial(n) {\n  if (n <= 1) {\n    return 1;\n  }\n  return n * factorial(n - 1);\n}",
    "python": "def factorial(n):\n    if n <= 1:\n        return 1\n\n    return n * factorial(n - 1)\n",
    "java": "int factorial(int n) {\n  if (n <= 1) {\n    return 1;\n  }\n  return n * factorial(n - 1);\n}",
    "cpp": "int factorial(int n) {\n  if (n <= 1) {\n    return 1;\n  }\n  return n * factorial(n - 1);\n}"
  },
  "defaultInput": [
    5
  ],
  "defaultTarget": null,
  "inputHint": "Type one number from 1 to 10. Watch the calls pile up before any answer appears.",
  "pitfalls": [
    {
      "wrong": "Writing a recursive function with no base case.",
      "right": "Without a case that returns without recursing, the calls never stop. The stack fills and the program dies. The base case is not optional — it's the floor."
    },
    {
      "wrong": "Recursing on the same problem instead of a smaller one.",
      "right": "factorial(n) must call factorial(n − 1), not factorial(n). Every call has to move closer to the base case, or you never reach it."
    },
    {
      "wrong": "Believing the multiplication happens on the way down.",
      "right": "It happens on the way back up. Nothing is computed until the base case returns — which is exactly what the call stack visualization shows you."
    },
    {
      "wrong": "Assuming recursion is free because the code is short.",
      "right": "Every pending call takes stack space. Deep recursion on a big input can overflow where a simple loop would happily run forever."
    }
  ],
  "quiz": [
    {
      "q": "What is the base case for?",
      "options": [
        "To make the code shorter",
        "To stop the recursion — it returns without calling itself",
        "To handle errors",
        "To speed things up"
      ],
      "answer": 1,
      "why": "It's the floor. It's the one case that answers directly instead of asking again — without it, the calls never stop and the stack overflows."
    },
    {
      "q": "In factorial(5), when does the first actual multiplication happen?",
      "options": [
        "Immediately, in factorial(5)",
        "After factorial(1) returns 1",
        "Halfway down, at factorial(3)",
        "Multiplications happen on the way down"
      ],
      "answer": 1,
      "why": "Nothing can be computed until the base case speaks. All five calls pile up waiting, then the answers are built on the way back up."
    },
    {
      "q": "Why does recursion use O(n) space when a loop uses O(1)?",
      "options": [
        "Recursive code is longer",
        "Each waiting call keeps a frame on the call stack",
        "It copies the whole input every time",
        "It doesn't — they use the same space"
      ],
      "answer": 1,
      "why": "Every call that hasn't returned yet is still sitting on the stack, holding its own variables. n pending calls means n frames of memory."
    }
  ],
  "problems": [
    {
      "name": "Fibonacci Number",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/fibonacci-number/"
    },
    {
      "name": "Climbing Stairs",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/climbing-stairs/"
    },
    {
      "name": "Pow(x, n)",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/powx-n/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 11,
    "frames": [
      {
        "data": {
          "stack": [],
          "returned": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {
          "n": 5
        },
        "explanation": "factorial(5) means 5 × 4 × 3 × 2 × 1. Watch how the computer gets there — it is not the way you'd do it by hand.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "calling"
            }
          ],
          "returned": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 5,
        "variables": {
          "n": 5,
          "depth": 1
        },
        "explanation": "factorial(5) cannot answer yet — it needs factorial(4) first. So it parks itself on the stack and asks.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "calling"
            },
            {
              "id": "f4",
              "label": "factorial(4)",
              "state": "calling"
            }
          ],
          "returned": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 5,
        "variables": {
          "n": 4,
          "depth": 2
        },
        "explanation": "factorial(4) cannot answer yet — it needs factorial(3) first. So it parks itself on the stack and asks.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "calling"
            },
            {
              "id": "f4",
              "label": "factorial(4)",
              "state": "calling"
            },
            {
              "id": "f3",
              "label": "factorial(3)",
              "state": "calling"
            }
          ],
          "returned": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 5,
        "variables": {
          "n": 3,
          "depth": 3
        },
        "explanation": "factorial(3) cannot answer yet — it needs factorial(2) first. So it parks itself on the stack and asks.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "calling"
            },
            {
              "id": "f4",
              "label": "factorial(4)",
              "state": "calling"
            },
            {
              "id": "f3",
              "label": "factorial(3)",
              "state": "calling"
            },
            {
              "id": "f2",
              "label": "factorial(2)",
              "state": "calling"
            }
          ],
          "returned": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 5,
        "variables": {
          "n": 2,
          "depth": 4
        },
        "explanation": "factorial(2) cannot answer yet — it needs factorial(1) first. So it parks itself on the stack and asks.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "calling"
            },
            {
              "id": "f4",
              "label": "factorial(4)",
              "state": "calling"
            },
            {
              "id": "f3",
              "label": "factorial(3)",
              "state": "calling"
            },
            {
              "id": "f2",
              "label": "factorial(2)",
              "state": "calling"
            },
            {
              "id": "f1",
              "label": "factorial(1)",
              "state": "returning",
              "result": 1
            }
          ],
          "returned": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 3,
        "variables": {
          "n": 1,
          "depth": 5,
          "returns": 1
        },
        "explanation": "factorial(1) is the base case. It does not need anyone else — it just knows the answer is 1. This is what stops the recursion falling forever.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "calling"
            },
            {
              "id": "f4",
              "label": "factorial(4)",
              "state": "calling"
            },
            {
              "id": "f3",
              "label": "factorial(3)",
              "state": "calling"
            }
          ],
          "returned": [
            {
              "id": "f1",
              "label": "factorial(1)",
              "state": "returning",
              "result": 1
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 5,
        "variables": {
          "n": 2,
          "returns": 2,
          "depth": 3
        },
        "explanation": "factorial(1) came back with 1. Now factorial(2) can finish: 2 × 1 = 2.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "calling"
            },
            {
              "id": "f4",
              "label": "factorial(4)",
              "state": "calling"
            }
          ],
          "returned": [
            {
              "id": "f1",
              "label": "factorial(1)",
              "state": "returning",
              "result": 1
            },
            {
              "id": "f2",
              "label": "factorial(2)",
              "state": "returning",
              "result": 2
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 5,
        "variables": {
          "n": 3,
          "returns": 6,
          "depth": 2
        },
        "explanation": "factorial(2) came back with 2. Now factorial(3) can finish: 3 × 2 = 6.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "calling"
            }
          ],
          "returned": [
            {
              "id": "f1",
              "label": "factorial(1)",
              "state": "returning",
              "result": 1
            },
            {
              "id": "f2",
              "label": "factorial(2)",
              "state": "returning",
              "result": 2
            },
            {
              "id": "f3",
              "label": "factorial(3)",
              "state": "returning",
              "result": 6
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 5,
        "variables": {
          "n": 4,
          "returns": 24,
          "depth": 1
        },
        "explanation": "factorial(3) came back with 6. Now factorial(4) can finish: 4 × 6 = 24.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [],
          "returned": [
            {
              "id": "f1",
              "label": "factorial(1)",
              "state": "returning",
              "result": 1
            },
            {
              "id": "f2",
              "label": "factorial(2)",
              "state": "returning",
              "result": 2
            },
            {
              "id": "f3",
              "label": "factorial(3)",
              "state": "returning",
              "result": 6
            },
            {
              "id": "f4",
              "label": "factorial(4)",
              "state": "returning",
              "result": 24
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 5,
        "variables": {
          "n": 5,
          "returns": 120,
          "depth": 0
        },
        "explanation": "factorial(4) came back with 24. Now factorial(5) can finish: 5 × 24 = 120.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "stack": [],
          "returned": [
            {
              "id": "f1",
              "label": "factorial(1)",
              "state": "returning",
              "result": 1
            },
            {
              "id": "f2",
              "label": "factorial(2)",
              "state": "returning",
              "result": 2
            },
            {
              "id": "f3",
              "label": "factorial(3)",
              "state": "returning",
              "result": 6
            },
            {
              "id": "f4",
              "label": "factorial(4)",
              "state": "returning",
              "result": 24
            },
            {
              "id": "f5",
              "label": "factorial(5)",
              "state": "returning",
              "result": 120
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {
          "answer": 120,
          "calls": 5
        },
        "explanation": "factorial(5) = 120. Notice the shape: 5 calls piled up doing nothing, then every answer was built on the way back up. That pile is the call stack — and if it never reaches a base case, it overflows.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      }
    ]
  }
}