Factorial Recursion Visualizer
Watch how factorial function calls itself recursively and see the call stack in action.
Call Stack: O(n)
Time: O(n)
Result: 120
Controls
factorial(5) = 120
Calls needed: 5
Medium
Step Information
Progress:1 / 0
Phase:📞 Making Calls
Pending:0 calls waiting
Click Start to begin the factorial visualization
Call Stack Visualization
Calling
Stack size: 0
Click Start to see the call stack in action
Execution Trace
Execution trace will appear here
Legend
Making function call
Returning value
Currently active
1
Stack levelPython Implementation
def factorial(n):
    # Base case: factorial of 0 or 1 is 1
    if n <= 1:
        return 1
    
    # Recursive case: n * factorial(n-1)
    return n * factorial(n - 1)
# Example usage
result = factorial(5)  # Returns 120JavaScript Implementation
function factorial(n) {
    // Base case
    if (n <= 1) {
        return 1;
    }
    
    // Recursive case
    return n * factorial(n - 1);
}
// Example usage
const result = factorial(5); // Returns 120Complexity Analysis & Key Insights
Time Complexity: O(n)
The function makes exactly n recursive calls. Each call performs constant time operations (comparison and multiplication), so total time is proportional to n.
For n=5: 5 function calls needed
Space Complexity: O(n)
The recursion depth equals n, meaning the call stack grows to size n. Each stack frame stores the parameter and return address.
Max stack depth: 5 frames
Key Patterns
- • 🎯 Base case: factorial(1) = 1
 - • 🔄 Recursive: n × factorial(n-1)
 - • 📞 Stack grows during calls
 - • 🔙 Values computed on return
 - • ⏳ Calls wait for subcalls
 - • 🧮 Final result: 120