Skip to content

Conversation

@berkkaradalan
Copy link
Contributor

Challenge 2 Solution

Submitted by: @berkkaradalan
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

  • Added solution file to challenge-2/submissions/berkkaradalan/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 the Challenge 2 submissions directory implementing a string reversal program. It includes a ReverseString function that reverses input strings by iterating over runes, and a main function that reads a line from standard input, reverses it, and prints the result.

Changes

Cohort / File(s) Summary
Challenge 2 String Reversal Submission
challenge-2/submissions/berkkaradalan/solution-template.go
Adds complete Go program with ReverseString(s string) string function that reverses strings by prepending runes, and main function that reads stdin, calls ReverseString, and prints output

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

  • Single file addition with straightforward, non-complex logic
  • Basic string reversal implementation using rune iteration
  • No error handling or edge case considerations beyond standard input reading

Possibly related PRs

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add solution for Challenge 2 by berkkaradalan' clearly describes the main change: adding a solution file for Challenge 2.
Description check ✅ Passed The description is related to the changeset, providing context about the Challenge 2 submission, including what was added 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: 0

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

9-21: Consider adding error handling for scanner.

While the happy path works correctly, it's a Go best practice to check for scanner errors after reading input. If Scan() encounters an I/O error, it should be reported.

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

26-26: Variable name i is misleading.

The variable i contains the rune value, not an index. Consider renaming it to r, char, or rune for better clarity.

Apply this diff:

 	var reversedString string
-	for _, i := range s {
-		reversedString = string(i) + reversedString
+	for _, r := range s {
+		reversedString = string(r) + reversedString
 	}

24-30: Great Unicode handling! Consider using strings.Builder for better performance.

The function correctly handles Unicode characters 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 better performance with larger inputs, 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 reversedString string
-	for _, i := range s {
-		reversedString = string(i) + reversedString
+	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 reversedString
+	return string(runes)
 }
📜 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 86c0d01.

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

1-7: LGTM!

The package declaration and imports are appropriate for reading from standard input and printing output.

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