Skip to content

Conversation

@Johrespi
Copy link
Contributor

Challenge 2 Solution

Submitted by: @Johrespi
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

  • Added solution file to challenge-2/submissions/Johrespi/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 Oct 31, 2025

Walkthrough

A new Go solution file is added to the Challenge 2 submissions directory. It implements an exported ReverseString function that reverses input strings by iterating backwards and concatenating characters, with a main program that reads a line from standard input, reverses it, and prints the result.

Changes

Cohort / File(s) Summary
Challenge 2 Johrespi Submission
challenge-2/submissions/Johrespi/solution-template.go
Adds new Go solution implementing exported ReverseString(s string) string function and CLI program to read, reverse, and output strings from standard input

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Verify the string reversal logic correctly handles the input-to-output flow
  • Confirm the main function properly reads from standard input and invokes ReverseString

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 pull request title "Add solution for Challenge 2 by Johrespi" is directly related to the main change in the changeset, which adds a new solution file (solution-template.go) for Challenge 2. The title is clear, concise, and specifically identifies the challenge number and contributor, making it easy for a reviewer scanning the history to understand that this PR introduces a Challenge 2 solution submission.
Description Check ✅ Passed The pull request description is related to the changeset and provides appropriate context for the submission. It clearly indicates that this is a Challenge 2 solution submission by Johrespi, references the solution file added at challenge-2/submissions/Johrespi/solution-template.go, and includes relevant details about testing and code quality. The description accurately aligns with what the changeset contains.

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 (1)
challenge-2/submissions/Johrespi/solution-template.go (1)

9-21: Consider checking for scanner errors.

The scanner could encounter an error while reading input. While the current implementation may be acceptable for this challenge, it's good practice to check scanner.Err() after the scan loop.

Example:

 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)
+	}
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9726d4c and 95ca627.

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

Comment on lines +24 to +31
func ReverseString(s string) string {
// TODO: Implement the function
var output string
for c := len(s) - 1; c >= 0; c-- {
output += string(s[c])
}
return 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 | 🔴 Critical

Fix critical Unicode bug: byte indexing breaks multi-byte UTF-8 characters.

The current implementation treats the string as a byte array by indexing with s[c]. This corrupts multi-byte UTF-8 characters like emojis, accented letters, or non-ASCII text. For example, reversing "café" or "Hello 👋" will produce garbled output.

Additionally, string concatenation in a loop has O(n²) time complexity because strings are immutable in Go.

Apply this diff to fix both issues:

-// ReverseString returns the reversed string of s.
 func ReverseString(s string) string {
-	// TODO: Implement the function
-	var output string
-	for c := len(s) - 1; c >= 0; c-- {
-	    output += string(s[c])
-	}
-	return output
+	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 solution:

  • Converts the string to a rune slice to properly handle Unicode characters
  • Uses in-place swapping for O(n) time complexity
  • Removes the obsolete TODO comment

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In challenge-2/submissions/Johrespi/solution-template.go around lines 24 to 31,
the current implementation indexes the string by bytes and concatenates in a
loop, which corrupts multi-byte UTF-8 characters and is O(n²); fix by converting
the input string to a rune slice, perform in-place swapping of runes
(two-pointer swap from ends toward center) to reverse in O(n) time, then return
a new string created from the rune slice, and remove the obsolete TODO comment.

@RezaSi RezaSi merged commit 2d72ac3 into RezaSi:main Oct 31, 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