Bird
Raised Fist0
DSA Goprogramming~20 mins

String Reversal Approaches in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of reversing a string using rune slice
What is the output of this Go code that reverses a string containing Unicode characters?
DSA Go
package main
import "fmt"
func reverse(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}
func main() {
    fmt.Println(reverse("Go语言"))
}
A语言Go
BoG言语
CGo语言
D言语oG
Attempts:
2 left
💡 Hint
Remember that Go strings are UTF-8 encoded, so converting to rune slice is needed to reverse Unicode characters correctly.
❓ Predict Output
intermediate
2:00remaining
Output of reversing string using byte slice (ASCII only)
What is the output of this Go code that reverses a string using a byte slice?
DSA Go
package main
import "fmt"
func reverse(s string) string {
    bytes := []byte(s)
    for i, j := 0, len(bytes)-1; i < j; i, j = i+1, j-1 {
        bytes[i], bytes[j] = bytes[j], bytes[i]
    }
    return string(bytes)
}
func main() {
    fmt.Println(reverse("hello"))
}
Ahelol
Bhello
Colleh
Derror
Attempts:
2 left
💡 Hint
This method works well for ASCII strings because each character is one byte.
🔧 Debug
advanced
2:00remaining
Identify the error in this string reversal code
What error does this Go code produce when trying to reverse a string?
DSA Go
package main
import "fmt"
func reverse(s string) string {
    for i := 0; i < len(s)/2; i++ {
        s[i], s[len(s)-1-i] = s[len(s)-1-i], s[i]
    }
    return s
}
func main() {
    fmt.Println(reverse("hello"))
}
Acompiles and prints 'olleh'
Bcannot assign to s[i] (strings are immutable)
Cindex out of range panic
Druntime error: invalid memory address
Attempts:
2 left
💡 Hint
Strings in Go cannot be changed by index because they are immutable.
🧠 Conceptual
advanced
2:00remaining
Why use rune slice instead of byte slice for string reversal?
Which reason best explains why reversing a string with Unicode characters requires converting to a rune slice instead of a byte slice in Go?
ABecause Unicode characters can be multiple bytes, reversing bytes breaks characters
BBecause byte slices cannot be converted back to strings
CBecause rune slices use less memory than byte slices
DBecause byte slices are slower than rune slices
Attempts:
2 left
💡 Hint
Think about how Unicode characters are stored in UTF-8 encoding.
🚀 Application
expert
3:00remaining
Output of reversing a string with mixed ASCII and Unicode characters
What is the output of this Go program that reverses the string "Go语言123" using rune slice reversal?
DSA Go
package main
import "fmt"
func reverse(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}
func main() {
    fmt.Println(reverse("Go语言123"))
}
A321言语oG
B321语言oG
CGo言语123
D321言语Go
Attempts:
2 left
💡 Hint
Remember that rune slice reversal reverses each Unicode character as a unit.

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

  1. Step 1: Understand the function name and purpose

    The function name Reverse suggests it changes the order of characters.
  2. Step 2: Interpret the expected behavior

    Reversing a string means the last character becomes first, and so on.
  3. Final Answer:

    It returns the input string with characters in reverse order. -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Option A correctly reverses the string. -> Option A
  4. 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

  1. 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.
  2. Step 2: Build the reversed string

    Characters appended: 'o', 'l', 'l', 'e', 'h' forming "olleh".
  3. Final Answer:

    olleh -> Option A
  4. Quick Check:

    Reverse of "hello" is "olleh" [OK]
Hint: Reverse string by appending chars from end to start [OK]
Common Mistakes:
  • Reading string forward instead of backward
  • Printing original string by mistake
  • Expecting error due to string indexing
4. Find the error in this Go code that tries to reverse a string s:
func reverse(s string) string {
  var reversed string
  for i := 0; i <= len(s); i++ {
    reversed += string(s[len(s)-i])
  }
  return reversed
}
medium
A. The function should return reversed as a byte slice.
B. The string concatenation should use +=.
C. The loop condition should be i < len(s), not <=.
D. The loop should start from len(s) and go down to 0.

Solution

  1. Step 1: Check loop bounds

    The loop runs from 0 to len(s) inclusive, but string indices go from 0 to len(s)-1.
  2. Step 2: Identify out-of-range index

    Accessing s[len(s)-i] when i=0 is s[len(s)], which is out of range and causes runtime error.
  3. Final Answer:

    The loop condition should be i < len(s), not <=. -> Option C
  4. Quick Check:

    Index must be within 0 to len(s)-1 [OK]
Hint: Loop index must stay inside string length bounds [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Accessing index len(s) which is invalid
  • Confusing string and byte slice types
5. Given a string with Unicode characters like "Go语言", which approach correctly reverses it preserving characters properly in Go?
hard
A. Split string by spaces and reverse the words.
B. Use a byte slice and reverse bytes directly.
C. Use strings.ToUpper and then reverse.
D. Convert string to []rune, reverse the rune slice, then convert back to string.

Solution

  1. Step 1: Understand Unicode in Go strings

    Go strings are byte sequences; Unicode characters may be multiple bytes.
  2. Step 2: Use rune slice to handle Unicode characters

    Converting to []rune treats each Unicode character as one element, safe to reverse.
  3. Step 3: Reverse rune slice and convert back

    Reversing []rune reverses characters correctly, then convert back to string.
  4. Final Answer:

    Convert string to []rune, reverse runes, then back to string. -> Option D
  5. Quick Check:

    Use runes for Unicode-safe reversal [OK]
Hint: Use []rune to reverse Unicode strings safely [OK]
Common Mistakes:
  • Reversing bytes breaks Unicode characters
  • Using uppercase conversion instead of reversing
  • Reversing words instead of characters
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