Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the sum of elements in the array recursively.
DSA Go
func sumArray(arr []int, n int) int {
if n == 0 {
return 0
}
return arr[n-1] + [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using n+1 causes infinite recursion.
Calling sumArray with n does not reduce the problem size.
✗ Incorrect
The function calls itself with n-1 to sum all elements up to index n-1 recursively.
2fill in blank
mediumComplete the code to check if a string is a palindrome using recursion.
DSA Go
func isPalindrome(s string, start int, end int) bool {
if start >= end {
return true
}
if s[start] != s[end] {
return false
}
return [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Moving indices in the wrong direction causes incorrect checks.
Not moving both indices causes infinite recursion.
✗ Incorrect
The function moves inward by increasing start and decreasing end to check characters recursively.
3fill in blank
hardFix the error in the recursive function to find the maximum element in an array.
DSA Go
func maxElement(arr []int, n int) int {
if n == 1 {
return arr[0]
}
maxRest := maxElement(arr, [1])
if arr[n-1] > maxRest {
return arr[n-1]
}
return maxRest
}Attempts:
3 left
💡 Hint
Common Mistakes
Using n+1 causes index out of range error.
Using n causes infinite recursion.
✗ Incorrect
The function reduces the problem size by calling itself with n-1 to find max in smaller array.
4fill in blank
hardFill both blanks to create a recursive function that counts occurrences of a character in a string.
DSA Go
func countChar(s string, c byte, index int) int {
if index == [1] {
return 0
}
count := 0
if s[index] == [2] {
count = 1
}
return count + countChar(s, c, index+1)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index instead of len(s) causes infinite recursion.
Comparing with wrong variable causes incorrect count.
✗ Incorrect
The base case is when index reaches string length; compare s[index] with character c.
5fill in blank
hardFill both blanks to implement a recursive function that reverses a string.
DSA Go
func reverseString(s string) string {
if len(s) == 0 {
return ""
}
return string(s[len(s)-1]) + [1](s[2]1)
}Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect slicing syntax causes compile errors.
Not calling reverseString recursively causes infinite recursion.
✗ Incorrect
The function takes last character and adds reversed substring excluding last character.
