Challenge - 5 Problems
String Mastery Across Languages
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateWhat is the output of this Go code with string indexing?
Consider the following Go code that indexes a string containing Unicode characters. What will be printed?
DSA Go
package main import "fmt" func main() { s := "Goπ»" fmt.Printf("%d\n", s[2]) }
Attempts:
2 left
π‘ Hint
Remember that indexing a string in Go returns a byte, not a rune.
β Incorrect
In Go, strings are byte slices. The emoji π» is a multi-byte UTF-8 character. Indexing s[2] returns the third byte of the string, which is 240 in decimal.
β Predict Output
intermediateWhat is the output of this Python string slicing code?
Given the Python code below, what is the output?
DSA Go
s = 'cafΓ©' print(s[3])
Attempts:
2 left
π‘ Hint
Python strings are sequences of Unicode characters.
β Incorrect
Python strings are Unicode sequences. Index 3 corresponds to the character 'Γ©'.
π§ Debug
advancedWhy does this JavaScript string length differ from expected?
Consider this JavaScript code snippet. Why does the length property not match the number of visible characters?
DSA Go
const s = 'π©π©π§π¦';
console.log(s.length);Attempts:
2 left
π‘ Hint
JavaScript strings use UTF-16 encoding internally.
β Incorrect
JavaScript's length counts UTF-16 code units, so complex emojis made of multiple code units have length greater than 1.
π§ Conceptual
advancedHow do Go and Python differ in string mutability?
Which statement correctly describes the difference in string mutability between Go and Python?
Attempts:
2 left
π‘ Hint
Think about whether you can change characters inside a string directly.
β Incorrect
Both Go and Python strings cannot be changed after creation; they are immutable.
π Application
expertHow to correctly iterate over Unicode characters in Go?
Given the Go string s := "Hello, δΈη", which code snippet correctly prints each Unicode character on a new line?
Attempts:
2 left
π‘ Hint
Use range to iterate over runes, not bytes.
β Incorrect
Using 'for _, r := range s' iterates over runes (Unicode code points), correctly handling multi-byte characters.
