Skip to content

Conversation

@es-codigo
Copy link
Contributor

Challenge 2 Solution

Submitted by: @es-codigo
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

  • Added solution file to challenge-2/submissions/es-codigo/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 5, 2025

Walkthrough

A new Go program is added to challenge-2/submissions/es-codigo/solution-template.go that implements string reversal. The module exports a ReverseString function that reverses input by iterating over runes and prepending them, with a main function that reads a line from stdin, reverses it, and prints the result.

Changes

Cohort / File(s) Summary
New string reversal solution
challenge-2/submissions/es-codigo/solution-template.go
Added ReverseString(s string) string function that reverses strings by iterating over runes and prepending to result. Added main function that reads input from stdin using scanner and outputs reversed string.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Single new file with straightforward logic
  • Rune-based string reversal is a common pattern, easy to verify for correctness
  • Minimal error handling (only checks scanner.Scan())
  • No complex control flow or dependencies to trace

Possibly related PRs

  • #593: Adds exported ReverseString(s string) function and main program for challenge-2 in a different submitter file
  • #639: Implements the same exported ReverseString function with main reading input and printing reversed string
  • #620: Adds ReverseString(s string) string to challenge-2 submissions with identical function signature and purpose

Pre-merge checks

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a Challenge 2 solution from the es-codigo user.
Description check ✅ Passed The description is directly related to the changeset, clearly explaining the Challenge 2 solution submission with details about what was added 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: 0

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

9-21: Consider checking scanner errors for completeness.

After the scanner.Scan() block, it's good practice to check scanner.Err() to catch any errors that occurred during scanning, though it's unlikely to fail in this context.

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)
+	}
 }

23-32: Optimize string reversal with strings.Builder.

The current implementation correctly handles Unicode by iterating over runes. However, string concatenation in a loop creates intermediate strings, resulting in O(n²) time complexity. Using strings.Builder would improve performance to O(n).

Apply this diff to optimize the function:

+import "strings"
+
 // ReverseString returns the reversed string of s.
 func ReverseString(s string) string {
-	rs := ""
+	runes := []rune(s)
+	var builder strings.Builder
+	builder.Grow(len(s))
 
-	for _, char := range s {
-		rs = string(char) + rs
+	for i := len(runes) - 1; i >= 0; i-- {
+		builder.WriteRune(runes[i])
 	}
 
-	return rs
+	return builder.String()
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 231bd24 and 38b3a13.

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

@RezaSi RezaSi merged commit 127dafe into RezaSi:main Nov 6, 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