Skip to content

Conversation

@4m4x
Copy link
Contributor

@4m4x 4m4x commented Nov 22, 2025

Challenge 2 Solution

Submitted by: @4m4x
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

  • Added solution file to challenge-2/submissions/4m4x/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 Go solution file is added to challenge-2 that implements string reversal. The solution includes a main function that reads input from stdin and an exported ReverseString function that reverses strings by converting to runes, iterating backwards, and using strings.Join to reconstruct the result.

Changes

Cohort / File(s) Summary
Challenge 2 String Reversal Solution
challenge-2/submissions/4m4x/solution-template.go
Adds main function reading stdin and ReverseString(s string) string exported function that reverses input by converting to runes, iterating backwards, collecting fragments, and joining with strings.Join

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Verify the reversal algorithm correctly handles multi-byte UTF-8 characters via rune conversion
  • Confirm stdin reading and output formatting follow expected conventions
  • Check that the exported function signature matches the challenge specification

Possibly related PRs

  • #579: Adds the same exported function ReverseString(s string) string to package main for Challenge 2 with a similar solution structure
  • #752: Implements the same ReverseString export with a different reversal algorithm (rune-swap vs. fragment collection)
  • #639: Provides an alternative ReverseString implementation for the same challenge, allowing comparison between different string reversal approaches

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically identifies that this PR adds a solution for Challenge 2 by the author 4m4x, which aligns with the changeset containing a new solution file.
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.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

🧹 Nitpick comments (1)
challenge-2/submissions/4m4x/solution-template.go (1)

25-35: Consider a more efficient and idiomatic approach.

The current implementation builds a slice of single-character strings and joins them, which is inefficient. Consider these alternatives:

  1. Reverse runes in place (most efficient):
func ReverseString(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)
}
  1. Use strings.Builder (if you prefer the append approach):
func ReverseString(s string) string {
	runes := []rune(s)
	var builder strings.Builder
	builder.Grow(len(s))
	for i := len(runes) - 1; i >= 0; i-- {
		builder.WriteRune(runes[i])
	}
	return builder.String()
}

Also note:

  • Line 29: i >= 0 is more idiomatic than i > -1
  • Line 33: Shadowing the parameter s reduces clarity
📜 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 0b1e788.

📒 Files selected for processing (1)
  • challenge-2/submissions/4m4x/solution-template.go (1 hunks)

Comment on lines +10 to +22
func main() {
// Read input from standard input
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

Add error handling for scanner.

The code doesn't check for scanner errors after the scan operation. While scanner.Scan() returns false for both EOF and errors, you should check scanner.Err() to handle actual I/O errors properly.

Apply this diff to add error handling:

 func main() {
 	// Read input from standard input
 	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.Fprintf(os.Stderr, "Error reading input: %v\n", 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
func main() {
// Read input from standard input
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
input := scanner.Text()
// Call the ReverseString function
output := ReverseString(input)
// Print the result
fmt.Println(output)
}
}
func main() {
// Read input from standard input
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.Fprintf(os.Stderr, "Error reading input: %v\n", err)
os.Exit(1)
}
}
🤖 Prompt for AI Agents
In challenge-2/submissions/4m4x/solution-template.go around lines 10 to 22, the
code calls scanner.Scan() but does not check scanner.Err() afterward; add error
handling by calling err := scanner.Err() after the scan (or after the scanning
loop), and if err != nil write a descriptive message to stderr (e.g.,
fmt.Fprintln(os.Stderr, "input error:", err)) and exit with a non-zero status
(os.Exit(1)) so actual I/O errors are surfaced instead of being ignored.

Comment on lines +29 to +30
for i := len(runes) - 1; i > -1; i-- {
output = append(output, string(s[i]))
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 | 🔴 Critical

Critical bug: incorrect indexing breaks Unicode support.

Line 30 uses s[i] (byte index) instead of runes[i] (rune index), which defeats the purpose of converting to runes on line 26. This will corrupt multi-byte UTF-8 characters like emoji or non-Latin scripts.

For example, reversing "Hello世界" would produce corrupted output because s[i] slices the original string byte-by-byte, splitting multi-byte UTF-8 sequences.

Apply this diff to fix the bug:

 	for i := len(runes) - 1; i > -1; i-- {
-		output = append(output, string(s[i]))
+		output = append(output, string(runes[i]))
 	}
📝 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
for i := len(runes) - 1; i > -1; i-- {
output = append(output, string(s[i]))
for i := len(runes) - 1; i > -1; i-- {
output = append(output, string(runes[i]))
}
🤖 Prompt for AI Agents
In challenge-2/submissions/4m4x/solution-template.go around lines 29 to 30, the
loop currently appends string(s[i]) which indexes the original byte string and
corrupts multi-byte UTF-8 characters; change the append to use the rune slice
instead (append output with string(runes[i])) so each Unicode code point is
preserved when reversing, and remove any reliance on s[i] in this loop.

@RezaSi RezaSi merged commit ce2966a 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