Bird
Raised Fist0
DSA Goprogramming~20 mins

String Traversal and Character Access in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
String Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of rune traversal in Go string
What is the output of the following Go code that traverses a string using range and prints each rune's integer value?
DSA Go
package main
import "fmt"
func main() {
    s := "Go😊"
    for _, r := range s {
        fmt.Print(int(r), " ")
    }
}
A71 111 128522
B71 111 55357 56842
C71 111 63
D71 111 128522 32
Attempts:
2 left
πŸ’‘ Hint
Remember that range over a string in Go iterates over runes, not bytes.
❓ Predict Output
intermediate
2:00remaining
Indexing bytes vs runes in Go strings
What is the output of this Go code that prints bytes and runes of a string containing multibyte characters?
DSA Go
package main
import "fmt"
func main() {
    s := "Hi😊"
    fmt.Print("Bytes: ")
    for i := 0; i < len(s); i++ {
        fmt.Print(s[i], " ")
    }
    fmt.Print("\nRunes: ")
    for _, r := range s {
        fmt.Print(r, " ")
    }
}
A
Bytes: 72 105 63 
Runes: 72 105 128522 
B
Bytes: 72 105 128522 
Runes: 72 105 128522 
C
Bytes: 72 105 240 159 152 138 
Runes: 72 105 55357 56842 
D
Bytes: 72 105 240 159 152 138 
Runes: 72 105 128522 
Attempts:
2 left
πŸ’‘ Hint
Bytes represent raw UTF-8 encoding, runes represent Unicode code points.
🧠 Conceptual
advanced
1:30remaining
Why direct indexing on Go strings can be misleading
Why can using direct indexing (s[i]) on a Go string with multibyte characters lead to incorrect character access?
ABecause s[i] accesses bytes, not runes, so multibyte characters are split into multiple bytes.
BBecause s[i] returns runes, but runes are not Unicode characters.
CBecause Go strings are mutable and indexing changes the string.
DBecause s[i] returns the character's position, not its value.
Attempts:
2 left
πŸ’‘ Hint
Think about UTF-8 encoding and how Go stores strings.
❓ Predict Output
advanced
2:00remaining
Output of string slicing with multibyte characters in Go
What is the output of this Go code that slices a string containing an emoji?
DSA Go
package main
import "fmt"
func main() {
    s := "Hi😊"
    fmt.Println(s[0:2])
    fmt.Println(s[2:])
}
A
Hi
😊
B
Hi
panic: slicing out of range
C
Hi
οΏ½
D
Hi
�😊
Attempts:
2 left
πŸ’‘ Hint
Remember slicing works on bytes, not runes, so slicing inside a multibyte character can cause invalid UTF-8.
πŸ”§ Debug
expert
2:30remaining
Identify the error in rune counting code
What error will this Go code produce when counting runes in a string, and why?
DSA Go
package main
import "fmt"
func main() {
    s := "Hello😊"
    count := 0
    for i := 0; i < len(s); i++ {
        if s[i] >= 128 {
            count++
        }
    }
    fmt.Println(count)
}
AIt prints 1, correctly counting the emoji as one rune.
BIt prints 4, counting bytes with value >= 128, not runes.
CIt causes a runtime panic due to invalid indexing.
DIt prints 0, because s[i] is a rune and none are >= 128.
Attempts:
2 left
πŸ’‘ Hint
Consider what s[i] returns and how multibyte characters are encoded.

Practice

(1/5)
1. What does the expression len(s) return when s is a string in Go?
easy
A. The first character of the string
B. The number of characters (runes) in the string
C. The last character of the string
D. The number of bytes in the string

Solution

  1. Step 1: Understand what len does on strings in Go

    In Go, len(s) returns the number of bytes, not characters, because strings are byte slices.
  2. Step 2: Differentiate bytes and characters

    Characters (runes) can be multiple bytes, so len counts bytes, not runes.
  3. Final Answer:

    The number of bytes in the string -> Option D
  4. Quick Check:

    len(s) = bytes count [OK]
Hint: Remember: len counts bytes, not characters in Go strings [OK]
Common Mistakes:
  • Thinking len counts characters (runes)
  • Confusing bytes with characters
  • Assuming len returns a character
2. Which of the following is the correct way to access the third character of a string s in Go?
easy
A. s[3]
B. s[2]
C. s.charAt(2)
D. s.get(2)

Solution

  1. Step 1: Recall Go string indexing

    Go strings can be indexed like byte slices, starting at 0, so the third character is at index 2.
  2. Step 2: Check syntax options

    Only s[2] is valid Go syntax for accessing a byte at index 2.
  3. Final Answer:

    s[2] -> Option B
  4. Quick Check:

    Indexing starts at 0, third char = s[2] [OK]
Hint: Indexing starts at 0; third char is s[2] in Go [OK]
Common Mistakes:
  • Using 1-based indexing like s[3]
  • Using methods like charAt or get which don't exist in Go
  • Confusing bytes with runes
3. What is the output of the following Go code?
package main
import "fmt"
func main() {
  s := "GoLang"
  fmt.Println(string(s[1]))
}
medium
A. o
B. G
C. 1
D. Syntax error

Solution

  1. Step 1: Identify the character at index 1

    String "GoLang" indexed at 1 is byte for 'o'.
  2. Step 2: Convert byte to string for printing

    Using string(s[1]) converts byte 'o' to string "o".
  3. Final Answer:

    o -> Option A
  4. Quick Check:

    s[1] = 'o' printed as string [OK]
Hint: s[index] gives byte; convert to string to print char [OK]
Common Mistakes:
  • Printing byte as number instead of string
  • Confusing index 1 with 0
  • Expecting syntax error
4. What is wrong with this Go code snippet?
package main
import "fmt"
func main() {
  s := "hello"
  for i := 0; i <= len(s); i++ {
    fmt.Printf("%c", s[i])
  }
}
medium
A. Loop condition causes index out of range error
B. fmt.Printf format specifier is incorrect
C. String s is not initialized properly
D. for loop syntax is invalid

Solution

  1. Step 1: Analyze loop range

    The loop runs from i=0 to i=len(s) inclusive, but valid indices are 0 to len(s)-1.
  2. Step 2: Identify error cause

    At i=len(s), s[i] causes index out of range runtime error.
  3. Final Answer:

    Loop condition causes index out of range error -> Option A
  4. Quick Check:

    Index must be less than len(s) [OK]
Hint: Loop index must be < len(s), not <= len(s) [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Ignoring runtime index out of range error
  • Misunderstanding string indexing
5. Given a string s containing Unicode characters, which Go code snippet correctly prints each character (rune) on a new line?
hard
A. for i := 0; i < len(s); i++ { fmt.Println(string(s[i])) }
B. for i := range s { fmt.Println(s[i]) }
C. for _, r := range s { fmt.Println(string(r)) }
D. for i := 0; i <= len(s); i++ { fmt.Println(s[i]) }

Solution

  1. Step 1: Understand Unicode handling in Go strings

    Go strings are byte slices; indexing gives bytes, not runes. To get runes, use range loop.
  2. Step 2: Check each option

    for _, r := range s { fmt.Println(string(r)) } uses for _, r := range s which iterates runes correctly and prints each rune as string.
  3. Final Answer:

    for _, r := range s { fmt.Println(string(r)) } -> Option C
  4. Quick Check:

    Use range to iterate runes in string [OK]
Hint: Use range loop to get runes, not byte indexing [OK]
Common Mistakes:
  • Using byte indexing for Unicode characters
  • Looping with <= len(s)
  • Printing bytes directly without conversion
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