Skip to content

Conversation

@YESUBZERO
Copy link
Contributor

Challenge 2 Solution

Submitted by: @YESUBZERO
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

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

Walkthrough

A new Go submission file is added to Challenge 2 that implements a string reversal function and CLI. The ReverseString function reverses input strings by iterating over runes, and the main function reads a line from stdin and prints the reversed output.

Changes

Cohort / File(s) Change Summary
String Reversal CLI Implementation
challenge-2/submissions/YESUBZERO/solution-template.go
Adds ReverseString(s string) string function that reverses strings by iterating over runes in reverse order; adds main function that reads input from stdin and prints reversed output; imports bufio, fmt, and os

Sequence Diagram

sequenceDiagram
    participant Main
    participant Stdin as Standard Input
    participant ReverseString
    participant Stdout as Standard Output

    Main->>Stdin: Read line
    Stdin-->>Main: Input string
    Main->>ReverseString: ReverseString(input)
    ReverseString->>ReverseString: Iterate runes in reverse
    ReverseString-->>Main: Reversed string
    Main->>Stdout: Print result
Loading

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

  • Straightforward string reversal logic with rune iteration
  • Simple CLI pattern with no complex control flow
  • Single exported function with clear purpose

Possibly related PRs

Pre-merge checks

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding a solution for Challenge 2 by a specific user (YESUBZERO), which 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 claims.

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/YESUBZERO/solution-template.go (2)

25-25: Remove the TODO comment.

The function is fully implemented, so the TODO comment should be removed to avoid confusion.

Apply this diff:

-	// TODO: Implement the function
 	var result string

24-32: Consider using strings.Builder for better performance.

The function correctly handles Unicode by iterating over runes. However, string concatenation in a loop creates a new string allocation on each iteration, resulting in O(n²) time complexity. For Go best practices, consider using strings.Builder or a []rune slice.

Here's a more efficient approach using a rune slice:

+import "strings"
+
 // ReverseString returns the reversed string of s.
 func ReverseString(s string) string {
-	var result string
-	
-	for _, char := range s {
-	    result = string(char) + result
-	}
-	return result
+	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)
 }

Alternatively, using strings.Builder:

func ReverseString(s string) string {
	runes := []rune(s)
	var builder strings.Builder
	builder.Grow(len(runes))
	for i := len(runes) - 1; i >= 0; i-- {
		builder.WriteRune(runes[i])
	}
	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 6b862ca.

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

Comment on lines +9 to +21
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 errors.

The scanner can fail for reasons other than EOF (e.g., I/O errors). Without checking scanner.Err(), these errors are silently ignored, which could confuse users when input fails unexpectedly.

Apply this diff to add proper 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 skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In challenge-2/submissions/YESUBZERO/solution-template.go around lines 9 to 21,
the main function reads input with bufio.NewScanner but does not check for
scanner errors; after using scanner.Scan() add a call to scanner.Err(), and if
non-nil print a clear error to stderr (e.g., fmt.Fprintln(os.Stderr, "input
error:", err)) and exit with a non-zero status so I/O errors aren't silently
ignored.

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