📖 Scenario: You are working on a simple program that uses recursion to process arrays and strings. Recursion means a function calls itself to solve smaller parts of the problem until it reaches a simple case.Imagine you want to find the sum of numbers in a list and also reverse a word using recursion.
🎯 Goal: Build a Go program that uses recursion to find the sum of an array of integers and to reverse a string.
📋 What You'll Learn
Create an integer array called numbers with the exact values: 2, 4, 6, 8, 10
Create a string variable called word with the exact value: "hello"
Write a recursive function called sumArray that takes the array and returns the sum of its elements
Write a recursive function called reverseString that takes the string and returns the reversed string
Print the sum of the array and the reversed string exactly as shown in the final output
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like searching, sorting, and processing nested data structures.
💼 Career
Understanding recursion helps in coding interviews and solving complex problems efficiently in software development.
Progress0 / 4 steps
1
Create the data variables
Create an integer array called numbers with the exact values: 2, 4, 6, 8, 10. Also create a string variable called word with the exact value: "hello".
DSA Go
Hint
Use numbers := []int{2, 4, 6, 8, 10} to create the array and word := "hello" for the string.
2
Add recursive function to sum the array
Write a recursive function called sumArray that takes a slice of integers called arr and returns the sum of its elements as an int. Use the base case when the slice is empty to return 0.
DSA Go
Hint
Check if the slice length is 0, then return 0. Otherwise, return the first element plus the sum of the rest.
3
Add recursive function to reverse the string
Write a recursive function called reverseString that takes a string called s and returns the reversed string. Use the base case when the string is empty to return an empty string.
DSA Go
Hint
Check if the string length is 0, then return empty string. Otherwise, call reverseString on the substring from index 1 and add the first character at the end.
4
Print the results
In the main function, print the sum of the numbers array by calling sumArray(numbers). Then print the reversed word by calling reverseString(word). Use fmt.Println to print each result on its own line.
DSA Go
Hint
Use fmt.Println(sumArray(numbers)) and fmt.Println(reverseString(word)) to print the results.
Practice
(1/5)
1. What does a recursive function do when processing an array?
easy
A. It only processes the first element and stops
B. It loops through the array without calling itself
C. It creates a new array each time without recursion
D. It calls itself with a smaller part of the array until a base case is reached
Solution
Step 1: Understand recursion on arrays
A recursive function processes an array by calling itself with a smaller portion, usually by reducing the size or moving the index.
Step 2: Identify the base case
The recursion stops when the array is empty or the index reaches the end, preventing infinite calls.
Final Answer:
It calls itself with a smaller part of the array until a base case is reached -> Option D
Quick Check:
Recursion = smaller calls until base case [OK]
Hint: Recursion breaks problem into smaller parts until simple case [OK]
Common Mistakes:
Thinking recursion loops without reducing problem size
Ignoring the base case leading to infinite recursion
Confusing recursion with iteration
2. Which of the following is the correct syntax to define a recursive function in Go that processes a string?
easy
A. func recurse(s string) string { if len(s) == 0 { return "" } return string(s[0]) + recurse(s[1:]) }
B. func recurse(s string) string { for i := 0; i < len(s); i++ { return s[i] } }
C. func recurse(s string) { if len(s) == 0 { return } else { recurse(s) } }
D. func recurse(s string) string { return s + recurse(s) }
Solution
Step 1: Check function signature and base case
func recurse(s string) string { if len(s) == 0 { return "" } return string(s[0]) + recurse(s[1:]) } defines a function with a base case checking if string length is zero, returning empty string.
Step 2: Confirm recursive call reduces problem size
func recurse(s string) string { if len(s) == 0 { return "" } return string(s[0]) + recurse(s[1:]) } calls itself with substring s[1:], reducing string size each call, correctly building result.