Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Factorial Using Recursion in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Factorial Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Recursive Factorial Function Call
What is the output of the following Python code that calculates factorial recursively?
DSA Python
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(4))
A10
B120
C24
D1
Attempts:
2 left
💡 Hint
Remember factorial of 4 is 4 * 3 * 2 * 1.
❓ Predict Output
intermediate
2:00remaining
Output When Calling Factorial with Zero
What will be printed when the factorial function is called with 0?
DSA Python
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(0))
ANone
B1
C0
DError
Attempts:
2 left
💡 Hint
Factorial of zero is defined as 1.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Factorial Code
What error will this code produce when calling factorial(3)?
DSA Python
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n + 1)

print(factorial(3))
ARecursionError: maximum recursion depth exceeded
BValueError: invalid literal for int()
CTypeError: unsupported operand type(s)
DOutput: 6
Attempts:
2 left
💡 Hint
Check how the recursive call changes the argument.
❓ Predict Output
advanced
2:00remaining
Output of Factorial with Negative Input
What happens when factorial is called with a negative number?
DSA Python
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(-1))
ARecursionError: maximum recursion depth exceeded
B-1
C1
DTypeError
Attempts:
2 left
💡 Hint
Think about the base case and how negative numbers affect recursion.
🧠 Conceptual
expert
2:00remaining
Why is a Base Case Necessary in Recursive Factorial?
Why must a recursive factorial function have a base case like 'if n == 0: return 1'?
ATo convert the recursion into iteration
BTo speed up the calculation by caching results
CTo handle negative inputs correctly
DTo stop the recursion and prevent infinite calls
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.

Practice

(1/5)
1. What does the factorial of a number represent in mathematics?
easy
A. The product of all positive integers up to that number
B. The sum of all positive integers up to that number
C. The number multiplied by 2
D. The number divided by 2

Solution

  1. Step 1: Understand factorial definition

    Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.
  2. Step 2: Compare options with definition

    Only The product of all positive integers up to that number correctly states this product definition.
  3. Final Answer:

    The product of all positive integers up to that number -> Option A
  4. Quick Check:

    Factorial = product sequence [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
  • Confusing factorial with sum
  • Thinking factorial doubles the number
  • Mixing factorial with division
2. Which of the following is the correct base case for a recursive factorial function in Python?
easy
A. if n == 1: return n + 1
B. if n == 1: return 0
C. if n == 0: return 0
D. if n == 0: return 1

Solution

  1. Step 1: Identify base case for factorial

    Factorial of 0 is defined as 1, so base case should return 1 when n is 0.
  2. Step 2: Check options for correct base case

    if n == 0: return 1 returns 1 when n == 0, which is correct. Others return wrong values.
  3. Final Answer:

    if n == 0: return 1 -> Option D
  4. Quick Check:

    Base case n=0 returns 1 [OK]
Hint: Base case factorial(0) = 1 [OK]
Common Mistakes:
  • Returning 0 for factorial(0)
  • Using wrong base case condition
  • Returning n+1 instead of 1
3. What is the output of the following code?
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(4))
medium
A. 10
B. 16
C. 24
D. 120

Solution

  1. Step 1: Trace recursive calls for factorial(4)

    factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * factorial(0) factorial(0) = 1 (base case)
  2. Step 2: Calculate values bottom-up

    factorial(1) = 1 * 1 = 1 factorial(2) = 2 * 1 = 2 factorial(3) = 3 * 2 = 6 factorial(4) = 4 * 6 = 24
  3. Final Answer:

    24 -> Option C
  4. Quick Check:

    4! = 24 [OK]
Hint: Multiply down from n to 1 recursively [OK]
Common Mistakes:
  • Stopping recursion too early
  • Adding instead of multiplying
  • Wrong base case causing infinite recursion
4. Find the error in this recursive factorial function:
def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n+1)

print(factorial(3))
medium
A. The recursive call should use n-1, not n+1
B. The print statement should be outside the function
C. The function should return n + factorial(n-1)
D. The base case should be n == 0, not n == 1

Solution

  1. Step 1: Analyze recursive call parameter

    The function calls factorial(n+1), which increases n each time, causing infinite recursion.
  2. Step 2: Correct recursive call to decrement n

    It should call factorial(n-1) to move towards base case and stop recursion.
  3. Final Answer:

    The recursive call should use n-1, not n+1 -> Option A
  4. Quick Check:

    Recursion must approach base case by decreasing n [OK]
Hint: Recursive calls must reduce n to reach base case [OK]
Common Mistakes:
  • Increasing n in recursive call causing infinite loop
  • Wrong base case value
  • Adding instead of multiplying in return
5. How can you modify the recursive factorial function to handle negative inputs gracefully?
hard
A. Change base case to n == -1
B. Add a check to return None or error if n < 0 before recursion
C. Use absolute value of n in recursive call
D. Ignore negative inputs and proceed as usual

Solution

  1. Step 1: Understand factorial domain

    Factorial is not defined for negative numbers, so function should handle this case explicitly.
  2. Step 2: Add input validation before recursion

    Check if n < 0 and return None or raise error to avoid invalid recursion.
  3. Final Answer:

    Add a check to return None or error if n < 0 before recursion -> Option B
  4. Quick Check:

    Validate input before recursion [OK]
Hint: Check for negative input before recursion [OK]
Common Mistakes:
  • Using absolute value hides invalid input
  • Changing base case to negative number
  • Ignoring invalid inputs causes wrong results
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard