Bird
Raised Fist0
DSA Goprogramming~20 mins

Recursion Base Case and Recursive Case in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Recursion 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
What is the output of this Go code that calculates factorial using recursion?
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))
}
A24
B10
C120
D0
Attempts:
2 left
💡 Hint
Recall factorial of 4 is 4*3*2*1.
❓ Predict Output
intermediate
2:00remaining
Output of Recursive Sum Function
What does this Go program print when summing numbers from n down to 1 recursively?
DSA Go
package main
import "fmt"
func sum(n int) int {
    if n <= 0 {
        return 0
    }
    return n + sum(n-1)
}
func main() {
    fmt.Println(sum(3))
}
A0
B3
C6
D9
Attempts:
2 left
💡 Hint
Sum of 3 + 2 + 1 = ?
🔧 Debug
advanced
2:00remaining
Identify the Infinite Recursion Bug
Which option shows a recursive function that will cause infinite recursion and eventually a stack overflow?
DSA Go
func countdown(n int) {
    if n == 0 {
        fmt.Println("Done")
        return
    }
    fmt.Println(n)
    countdown(n)
}
A
func countdown(n int) {
    if n == 0 {
        fmt.Println("Done")
        return
    }
    fmt.Println(n)
    countdown(n-1)
}
B
func countdown(n int) {
    if n &lt;= 0 {
        fmt.Println("Done")
        return
    }
    fmt.Println(n)
    countdown(n)
}
C
func countdown(n int) {
    if n &lt; 0 {
        fmt.Println("Done")
        return
    }
    fmt.Println(n)
    countdown(n-1)
}
D
func countdown(n int) {
    if n == 0 {
        fmt.Println("Done")
        return
    }
    fmt.Println(n)
    countdown(n+1)
}
Attempts:
2 left
💡 Hint
Check if the recursive call changes the argument to approach the base case.
🧠 Conceptual
advanced
1:30remaining
Understanding Base Case Importance
Why is the base case essential in a recursive function?
AIt makes the function run faster by skipping steps.
BIt initializes variables before recursion starts.
CIt allows the function to call itself multiple times.
DIt stops the recursion to prevent infinite calls.
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
❓ Predict Output
expert
2:30remaining
Output of Recursive Fibonacci Function
What is the output of this Go program that calculates Fibonacci numbers recursively?
DSA Go
package main
import "fmt"
func fib(n int) int {
    if n <= 1 {
        return n
    }
    return fib(n-1) + fib(n-2)
}
func main() {
    fmt.Println(fib(5))
}
A5
B8
C13
D3
Attempts:
2 left
💡 Hint
Fibonacci sequence starts 0,1,1,2,3,5,...

Practice

(1/5)
1. What is the purpose of the base case in a recursive function?
easy
A. To print the result of the recursion
B. To call the function again with a smaller input
C. To increase the input size for the next call
D. To stop the recursion and prevent infinite calls

Solution

  1. Step 1: Understand recursion basics

    A recursive function calls itself repeatedly until a stopping condition is met.
  2. Step 2: Identify the role of base case

    The base case stops further recursive calls to avoid infinite loops.
  3. Final Answer:

    To stop the recursion and prevent infinite calls -> Option D
  4. Quick Check:

    Base case = stop recursion [OK]
Hint: Base case always stops recursion to avoid infinite loops [OK]
Common Mistakes:
  • Confusing base case with recursive case
  • Thinking base case calls function again
  • Ignoring base case leads to infinite recursion
2. Which of the following is the correct syntax for a base case in Go recursion to stop when n is zero?
easy
A. if n == 0 { return 0 }
B. if n = 0 { return 0 }
C. if n != 0 { return 0 }
D. if n > 0 { return 0 }

Solution

  1. Step 1: Check Go syntax for condition

    In Go, equality check uses '==' not '=' which is assignment.
  2. Step 2: Identify correct base case condition

    Base case stops recursion when n equals zero, so 'if n == 0 { return 0 }' is correct.
  3. Final Answer:

    if n == 0 { return 0 } -> Option A
  4. Quick Check:

    Use '==' for comparison in base case [OK]
Hint: Use '==' for comparison in base case conditions [OK]
Common Mistakes:
  • Using '=' instead of '==' for comparison
  • Wrong condition logic for base case
  • Returning wrong value in base case
3. What will be the output of this Go recursive function when called with factorial(3)?
func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n - 1)
}
medium
A. 6
B. 3
C. 0
D. 1

Solution

  1. Step 1: Trace recursive calls for 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 return values step-by-step

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

    6 -> Option A
  4. Quick Check:

    3! = 6 [OK]
Hint: Multiply n by factorial(n-1) until n=0 [OK]
Common Mistakes:
  • Forgetting base case returns 1
  • Multiplying incorrectly in recursive step
  • Confusing factorial with addition
4. Identify the error in this recursive Go function to compute the sum of numbers from 1 to n:
func sum(n int) int {
    if n == 1 {
        return 1
    }
    return n + sum(n)
}
medium
A. Base case condition is incorrect
B. Function should return n * sum(n-1)
C. Recursive call does not reduce n, causing infinite recursion
D. Return statement should be outside the if block

Solution

  1. Step 1: Analyze recursive call argument

    The function calls sum(n) again without reducing n, so it never reaches base case.
  2. Step 2: Identify infinite recursion cause

    Recursive call must use sum(n-1) to reduce n and eventually stop at base case.
  3. Final Answer:

    Recursive call does not reduce n, causing infinite recursion -> Option C
  4. Quick Check:

    Recursive argument must decrease to reach base case [OK]
Hint: Recursive calls must reduce input to reach base case [OK]
Common Mistakes:
  • Calling recursion with same argument
  • Wrong base case value
  • Misplacing return statement
5. Given this recursive Go function to reverse a string, what is the output of reverse("go")?
func reverse(s string) string {
    if len(s) == 0 {
        return ""
    }
    return reverse(s[1:]) + string(s[0])
}
hard
A. "go"
B. "og"
C. ""
D. Runtime error

Solution

  1. Step 1: Trace recursive calls for reverse("go")

    reverse("go") calls reverse("o") + "g" reverse("o") calls reverse("") + "o" reverse("") returns "" (base case)
  2. Step 2: Combine return values step-by-step

    reverse("") = "" reverse("o") = "" + "o" = "o" reverse("go") = "o" + "g" = "og"
  3. Final Answer:

    "og" -> Option B
  4. Quick Check:

    String reversed = "og" [OK]
Hint: Reverse substring then add first char at end [OK]
Common Mistakes:
  • Confusing substring slicing
  • Forgetting base case returns empty string
  • Concatenating in wrong order
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