Skip to content

Conversation

@inok94
Copy link
Contributor

@inok94 inok94 commented Nov 22, 2025

Challenge 2 Solution

Submitted by: @inok94
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

  • Added solution file to challenge-2/submissions/inok94/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 22, 2025

Walkthrough

A new submission file for Challenge 2 that implements a string reversal solution. The program reads a single line from standard input, reverses it using a ReverseString function with strings.Builder, and outputs the result.

Changes

Cohort / File(s) Summary
Challenge 2 Submission
challenge-2/submissions/inok94/solution-template.go
Adds a complete Go program with a ReverseString function that reverses strings by iterating from end to start via strings.Builder, and a main function that reads input from stdin and prints the reversed output

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Review the ReverseString implementation for correctness with edge cases (empty strings, multi-byte characters)
  • Verify the main function properly handles input from stdin and outputs the correct result
  • Check that the strings.Builder approach is efficiently sized and used correctly

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 directly and clearly describes the main change: adding a solution for Challenge 2 by a specific contributor (inok94).
Description check ✅ Passed The description is directly related to the changeset, clearly explaining that it contains a Challenge 2 solution submission with specific file changes and testing confirmation.

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: 1

🧹 Nitpick comments (2)
challenge-2/submissions/inok94/solution-template.go (2)

10-22: Consider adding error handling for scanner errors.

While the logic correctly handles the single-line input case, consider checking scanner.Err() after the scan to catch potential I/O errors. This would make the solution more robust.

You could add after line 21:

	if err := scanner.Err(); err != nil {
		fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
		os.Exit(1)
	}

29-29: Inconsistent indentation - run gofmt.

This line appears to use spaces for indentation instead of tabs. Run gofmt -w solution-template.go to automatically fix formatting according to Go conventions.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca8ea29 and 7e001d7.

📒 Files selected for processing (1)
  • challenge-2/submissions/inok94/solution-template.go (1 hunks)
🔇 Additional comments (1)
challenge-2/submissions/inok94/solution-template.go (1)

1-8: LGTM!

Package declaration and imports are appropriate for this string reversal program.

Comment on lines +25 to +33
func ReverseString(s string) string {
builder := strings.Builder{}
builder.Grow(len(s))
for i := len(s) - 1; i >= 0; i-- {
builder.WriteByte(s[i])
}

return builder.String()
}
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

Critical: Byte-level reversal breaks UTF-8 multi-byte characters.

The current implementation uses s[i] which indexes individual bytes, not Unicode code points (runes). This will corrupt multi-byte UTF-8 characters like emoji, Chinese characters, etc.

For example:

  • Input: "Hello世界" (where 世 and 界 are 3 bytes each)
  • Expected: "界世olleH"
  • Actual: Corrupted UTF-8 byte sequence

Apply this diff to correctly handle UTF-8:

 func ReverseString(s string) string {
-	builder := strings.Builder{}
-	builder.Grow(len(s))
-	for i := len(s) - 1; i >= 0; i-- {
-	    builder.WriteByte(s[i])
-	}
-	
-	return builder.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)
 }

This approach converts the string to a rune slice, reverses in-place by swapping, and converts back to a string.

🤖 Prompt for AI Agents
In challenge-2/submissions/inok94/solution-template.go around lines 25 to 33,
the function reverses bytes (using s[i]) which corrupts multi-byte UTF-8
characters; fix by converting the string to a []rune, reverse the rune slice
in-place by swapping runes from ends toward the center, then convert the rune
slice back to a string and return it so UTF-8 characters remain intact.

@RezaSi RezaSi merged commit 0ce9d2a into RezaSi:main Nov 23, 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