Challenge - 5 Problems
Factorial Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this factorial recursive function call?
Consider the following Go code that calculates factorial using recursion. What will be printed when calling factorial(4)?
DSA Go
package main import "fmt" func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } func main() { fmt.Println(factorial(4)) }
Attempts:
2 left
💡 Hint
Recall that factorial of 4 is 4 * 3 * 2 * 1.
✗ Incorrect
The factorial function multiplies n by factorial of n-1 until n is 0, where it returns 1. So factorial(4) = 4*3*2*1 = 24.
❓ Predict Output
intermediateWhat is the output of factorial(0) in this recursive function?
Given the factorial recursive function below, what will be the output of factorial(0)?
DSA Go
package main import "fmt" func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } func main() { fmt.Println(factorial(0)) }
Attempts:
2 left
💡 Hint
Factorial of zero is defined as 1.
✗ Incorrect
The base case returns 1 when n is 0, so factorial(0) returns 1.
🔧 Debug
advancedWhat error occurs when calling factorial(-1) with this code?
Analyze the following factorial recursive function. What happens if factorial(-1) is called?
DSA Go
package main import "fmt" func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } func main() { fmt.Println(factorial(-1)) }
Attempts:
2 left
💡 Hint
Think about what happens when n never reaches 0.
✗ Incorrect
Since n starts at -1 and decreases by 1 each call, it never reaches 0, causing infinite recursion and stack overflow.
🧠 Conceptual
advancedHow many times is factorial called when computing factorial(3)?
Using the recursive factorial function, how many total calls to factorial are made when computing factorial(3)?
DSA Go
func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) }
Attempts:
2 left
💡 Hint
Count the initial call plus each recursive call until base case.
✗ Incorrect
Calls: factorial(3), factorial(2), factorial(1), factorial(0) → total 4 calls.
🚀 Application
expertWhat is the output of this modified factorial function with memoization for factorial(5)?
Consider this Go code that uses recursion with memoization to compute factorial. What is printed when factorial(5) is called?
DSA Go
package main import "fmt" var memo = map[int]int{} func factorial(n int) int { if n == 0 { return 1 } if val, ok := memo[n]; ok { return val } memo[n] = n * factorial(n-1) return memo[n] } func main() { fmt.Println(factorial(5)) }
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
The function correctly computes factorial(5) = 120 and stores intermediate results in memo.
