Skip to content

Conversation

@kiramux
Copy link
Contributor

@kiramux kiramux commented Nov 10, 2025

Challenge 2 Solution

Submitted by: @kiramux
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

  • Added solution file to challenge-2/submissions/kiramux/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 10, 2025

Walkthrough

Adds a new Go submission file that implements a string reversal function using a two-pointer byte-swap approach. The ReverseString function converts input to bytes, reverses in-place, and returns the result. The main function reads a single line from standard input and prints the reversed output.

Changes

Cohort / File(s) Summary
Challenge 2 Submission
challenge-2/submissions/kiramux/solution-template.go
New file adding ReverseString(s string) string function with two-pointer byte-swap implementation and main function that reads input from stdin and prints reversed string

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify correctness of two-pointer swap logic for byte reversal
  • Confirm empty string edge case is handled properly
  • Check I/O handling with scanner and standard output

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a Challenge 2 solution by kiramux. It directly corresponds to the changeset.
Description check ✅ Passed The description is directly related to the changeset, providing context about the Challenge 2 solution submission, the file location, and testing status.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between accf97c and b487e40.

📒 Files selected for processing (1)
  • challenge-2/submissions/kiramux/solution-template.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
challenge-2/submissions/kiramux/solution-template.go (1)
challenge-2/submissions/0xarash/solution-template.go (1)
  • ReverseString (24-31)

Comment on lines +11 to +20
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
input := scanner.Text()

// Call the ReverseString function
output := ReverseString(input)

// Print the result
fmt.Println(output)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Check for scanner errors after reading input.

The scanner may encounter errors while reading from stdin. After scanner.Scan() returns false, you should check scanner.Err() to distinguish between EOF and actual errors.

Apply this diff to add error handling:

 	scanner := bufio.NewScanner(os.Stdin)
 	if scanner.Scan() {
 		input := scanner.Text()
 
 		// Call the ReverseString function
 		output := ReverseString(input)
 
 		// Print the result
 		fmt.Println(output)
 	}
+	if err := scanner.Err(); err != nil {
+		fmt.Fprintln(os.Stderr, "Error reading input:", err)
+		os.Exit(1)
+	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
input := scanner.Text()
// Call the ReverseString function
output := ReverseString(input)
// Print the result
fmt.Println(output)
}
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
input := scanner.Text()
// Call the ReverseString function
output := ReverseString(input)
// Print the result
fmt.Println(output)
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Error reading input:", err)
os.Exit(1)
}
🤖 Prompt for AI Agents
In challenge-2/submissions/kiramux/solution-template.go around lines 11 to 20,
the code calls scanner.Scan() but does not check scanner.Err() afterwards;
update the code so that after the Scan attempt you call scanner.Err() and if it
returns a non-nil error print a descriptive message to stderr (or log) and exit
with a non-zero status to signal failure, otherwise proceed normally (nil error
implies EOF/no input). Ensure the error handling runs when Scan() returns false
so real I/O errors are distinguished from EOF.

Comment on lines +24 to +37
func ReverseString(s string) string {
if len(s) == 0 {
return ""
}
str := []byte(s)
left := 0
right := len(str) - 1
for left < right {
str[left], str[right] = str[right], str[left]
left++
right--
}
return string(str)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Use []rune instead of []byte to correctly handle Unicode characters.

The current implementation converts the string to []byte, which reverses raw bytes rather than Unicode characters. This will corrupt multi-byte UTF-8 characters like emojis, Chinese characters, accented letters, etc.

For example:

  • Input: "Hello, 世界"
  • Expected: "界世 ,olleH"
  • Actual with []byte: Mangled output with broken UTF-8 sequences

Apply this diff to fix the Unicode handling:

 func ReverseString(s string) string {
-	if len(s) == 0 {
-		return ""
-	}
-	str := []byte(s)
-	left := 0
-	right := len(str) - 1
-	for left < right {
-		str[left], str[right] = str[right], str[left]
-		left++
-		right--
+	r := []rune(s)
+	for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
+		r[i], r[j] = r[j], r[i]
 	}
-	return string(str)
+	return string(r)
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func ReverseString(s string) string {
if len(s) == 0 {
return ""
}
str := []byte(s)
left := 0
right := len(str) - 1
for left < right {
str[left], str[right] = str[right], str[left]
left++
right--
}
return string(str)
}
func ReverseString(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
🤖 Prompt for AI Agents
In challenge-2/submissions/kiramux/solution-template.go around lines 24 to 37,
the function reverses raw bytes using []byte which corrupts multi-byte UTF-8
characters; change the implementation to convert the string to []rune, perform
the in-place two-pointer swap on runes, then return string(runes) so Unicode
characters (emojis, CJK, accented letters) are handled correctly.

@RezaSi RezaSi merged commit 7f38c45 into RezaSi:main Nov 11, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants