What is the simplest way to reverse a string in Go using built-in functions?
Convert the string to a slice of runes, reverse the slice, then convert it back to a string. This handles multi-byte characters correctly.
Click to reveal answer
beginner
Why should you convert a string to a rune slice before reversing in Go?
Because Go strings are UTF-8 encoded, and characters can be multiple bytes. Reversing bytes directly can break characters. Runes represent Unicode code points safely.
Click to reveal answer
intermediate
Explain the two-pointer approach to reverse a string.
Use two pointers: one at the start and one at the end of the rune slice. Swap the runes at these pointers and move them towards the center until they meet or cross.
Click to reveal answer
beginner
What is a common mistake when reversing strings in languages like Go?
Reversing the string byte-by-byte without considering multi-byte characters, which can corrupt Unicode characters.
Click to reveal answer
intermediate
How can recursion be used to reverse a string?
Recursively call the function with the substring excluding the first character, then append the first character at the end until the base case (empty string) is reached.
Click to reveal answer
In Go, what type should you convert a string to before reversing to handle Unicode correctly?
A[]byte
B[]int
C[]rune
Dstring
✗ Incorrect
Converting to []rune ensures each Unicode character is treated as a single element, avoiding corruption.
What does the two-pointer technique do when reversing a string?
ASwaps characters from start and end moving inward
BUses recursion to reverse the string
CConverts string to bytes and reverses
DAppends characters to a new string in order
✗ Incorrect
Two pointers start at opposite ends and swap characters moving towards the center.
Why is reversing a string byte-by-byte in Go risky?
AIt only works for ASCII
BIt is slower than rune reversal
CIt uses more memory
DIt can break multi-byte Unicode characters
✗ Incorrect
UTF-8 characters can be multiple bytes; reversing bytes can corrupt characters.
Which approach is NOT commonly used to reverse strings?
ATwo-pointer swapping
BSorting characters
CUsing built-in reverse functions
DRecursion
✗ Incorrect
Sorting changes order based on character value, not reversal.
What is the base case in recursive string reversal?
AString is empty
BString length is 1
CString length is even
DString contains only ASCII
✗ Incorrect
An empty string means no more characters to reverse.
Describe step-by-step how to reverse a string in Go safely considering Unicode characters.
Think about how Go handles strings and Unicode.
You got /3 concepts.
Explain why reversing a string byte-by-byte can cause problems in Go and how to avoid it.
Consider how characters are stored internally.
You got /3 concepts.
Practice
(1/5)
1. What does the strings.Reverse function do in Go if it existed? (Assume it reverses the string.)
easy
A. It converts the string to uppercase.
B. It returns the input string with characters in reverse order.
C. It removes all vowels from the string.
D. It splits the string into words.
Solution
Step 1: Understand the function name and purpose
The function name Reverse suggests it changes the order of characters.
Step 2: Interpret the expected behavior
Reversing a string means the last character becomes first, and so on.
Final Answer:
It returns the input string with characters in reverse order. -> Option B
Quick Check:
Reverse means flip order [OK]
Hint: Reverse means flip the order of characters [OK]
Common Mistakes:
Confusing reverse with uppercase conversion
Thinking reverse removes characters
Assuming reverse splits words
2. Which of the following Go code snippets correctly reverses a string s?
easy
A. var reversed string
for i := len(s)-1; i >= 0; i-- {
reversed += string(s[i])
}
B. for i := 0; i < len(s); i++ {
reversed += string(s[i])
}
C. reversed := strings.ToUpper(s)
D. reversed := strings.Split(s, " ")
Solution
Step 1: Analyze the loop direction
var reversed string
for i := len(s)-1; i >= 0; i-- {
reversed += string(s[i])
} loops from the last character to the first, building reversed string.
Step 2: Check other options
for i := 0; i < len(s); i++ {
reversed += string(s[i])
} loops forward, so no reversal. reversed := strings.ToUpper(s) changes case, reversed := strings.Split(s, " ") splits words.
Final Answer:
Option A correctly reverses the string. -> Option A
Quick Check:
Loop backward to reverse [OK]
Hint: Loop backward from end to start to reverse [OK]
Common Mistakes:
Looping forward instead of backward
Using string functions that don't reverse
Confusing splitting with reversing
3. What is the output of this Go code?
func main() {
s := "hello"
reversed := ""
for i := len(s) - 1; i >= 0; i-- {
reversed += string(s[i])
}
fmt.Println(reversed)
}
medium
A. olleh
B. hello
C. h
D. error
Solution
Step 1: Trace the loop from end to start
The loop starts at index 4 ('o') and goes to 0 ('h'), appending characters in reverse order.