Challenge - 5 Problems
Recursion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Sum of Array Elements
What is the output of this Go code that recursively sums elements of an array?
DSA Go
package main import "fmt" func recursiveSum(arr []int, index int) int { if index == len(arr) { return 0 } return arr[index] + recursiveSum(arr, index+1) } func main() { arr := []int{1, 2, 3, 4} fmt.Println(recursiveSum(arr, 0)) }
Attempts:
2 left
💡 Hint
Think about how the function adds each element starting from index 0 until the end.
✗ Incorrect
The function adds each element from index 0 to the end recursively. 1+2+3+4 = 10.
❓ Predict Output
intermediateOutput of Recursive String Reverse
What is the output of this Go code that recursively reverses a string?
DSA Go
package main import "fmt" func reverseString(s string) string { if len(s) == 0 { return "" } return reverseString(s[1:]) + string(s[0]) } func main() { fmt.Println(reverseString("go")) }
Attempts:
2 left
💡 Hint
The function takes the first character and puts it at the end after reversing the rest.
✗ Incorrect
The function reverses "go" to "og" by recursion.
🔧 Debug
advancedIdentify the Error in Recursive Palindrome Check
What error does this Go code produce when checking if a string is a palindrome recursively?
DSA Go
package main import "fmt" func isPalindrome(s string, start int, end int) bool { if start >= end { return true } if s[start] != s[end] { return false } return isPalindrome(s, start+1, end-1) } func main() { fmt.Println(isPalindrome("level", 0, len("level")-1)) }
Attempts:
2 left
💡 Hint
Check the end index passed to the function and how it is used for indexing.
✗ Incorrect
The end index is set to len(s) which is out of range for indexing (max index is len(s)-1).
🧠 Conceptual
advancedNumber of Recursive Calls in Fibonacci Calculation
How many times is the recursive function called when calculating fibonacci(4) using this naive recursive code?
DSA Go
func fibonacci(n int) int { if n <= 1 { return n } return fibonacci(n-1) + fibonacci(n-2) }
Attempts:
2 left
💡 Hint
Draw the call tree for fibonacci(4) and count all calls including repeated ones.
✗ Incorrect
The call tree has 9 calls total for fibonacci(4).
🚀 Application
expertOutput of Recursive Flattening of Nested Integer Slices
What is the output of this Go code that recursively flattens nested slices of integers?
DSA Go
package main import "fmt" func flatten(input interface{}) []int { result := []int{} switch v := input.(type) { case int: result = append(result, v) case []interface{}: for _, item := range v { result = append(result, flatten(item)...) } } return result } func main() { nested := []interface{}{1, []interface{}{2, 3}, 4, []interface{}{[]interface{}{5}, 6}} fmt.Println(flatten(nested)) }
Attempts:
2 left
💡 Hint
The function recursively checks if the item is int or slice and appends accordingly.
✗ Incorrect
The function collects all integers in order from nested slices into a flat slice.
