Skip to content

Conversation

@iamsurajmandal
Copy link
Contributor

Challenge 19 Solution

Submitted by: @iamsurajmandal
Challenge: Challenge 19

Description

This PR contains my solution for Challenge 19.

Changes

  • Added solution file to challenge-19/submissions/iamsurajmandal/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 7, 2025

Walkthrough

Adds a new Go solution file with four slice-manipulation utility functions (FindMax, RemoveDuplicates, ReverseSlice, FilterEven) and a main function demonstrating their usage on a test slice.

Changes

Cohort / File(s) Summary
New slice utility solution
challenge-19/submissions/iamsurajmandal/solution-template.go
Introduces four exported functions: FindMax returns the maximum value in a slice; RemoveDuplicates removes duplicates while preserving order; ReverseSlice reverses elements; FilterEven filters only even numbers. Main demonstrates each function on a sample integer slice.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Straightforward utility functions with clear, single-purpose logic
  • Minimal edge case handling (empty slice returns in FindMax)
  • No complex control flow or interactions between functions

Possibly related PRs

  • PR #601: Adds identical four slice-utility functions (FindMax, RemoveDuplicates, ReverseSlice, FilterEven) and main demonstration to another challenge-19 submission file, indicating parallel solutions.

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding a solution for Challenge 19 by a specific contributor. It is concise, specific, and directly related to the changeset.
Description check ✅ Passed The description is directly related to the changeset, providing context about the Challenge 19 solution submission, the file added, and testing status. It contains meaningful information about the PR.
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: 3

🧹 Nitpick comments (1)
challenge-19/submissions/iamsurajmandal/solution-template.go (1)

62-67: Consider pre-allocating the result slice.

Pre-allocating with the known length improves performance by avoiding multiple allocations during append operations.

-    result := []int{}
+    result := make([]int, 0, len(slice))
 	for i := len(slice) - 1; i >= 0 ; i-- {
 	    result = append(result, slice[i])
 	}

Alternatively, for even better performance, allocate with full length and assign by index:

func ReverseSlice(slice []int) []int {
    result := make([]int, len(slice))
	for i := 0; i < len(slice); i++ {
	    result[len(slice)-1-i] = slice[i]
	}
	return result
}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f811e38 and ed1c483.

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

8-27: LGTM!

The main function provides clear demonstrations of all utility functions with well-labeled output.


46-58: LGTM!

The deduplication logic correctly preserves order using a map to track seen values.


70-80: LGTM with minor style note.

The filtering logic correctly identifies even numbers. Minor: the parentheses around the condition on line 75 are unnecessary in Go (idiomatic style omits them for simple conditions).

Optional style refinement:

-	    if (value % 2 == 0 ) {
+	    if value % 2 == 0 {
 	        result = append(result, value)
 	    }

if (len(numbers) < 1) {
return 0
}
max := int(math.Inf(-1))
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: Avoid converting math.Inf to int.

Converting math.Inf(-1) (negative infinity) to int is problematic—the behavior is implementation-defined and can produce unexpected results. Initialize max with the first element after confirming the slice is non-empty, or use math.MinInt.

Apply this diff to use a clearer initialization:

 func FindMax(numbers []int) int {
-    if (len(numbers) < 1) {
+    if len(numbers) == 0 {
         return 0
     }
-	max := int(math.Inf(-1))
-    for i := 0; i < len(numbers); i++ {
-        if (numbers[i] > max) {
-            max = numbers[i]
-        }
-    }	
+	max := numbers[0]
+    for _, num := range numbers {
+        if num > max {
+            max = num
+        }
+    }
 	return max
 }

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

🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 35,
the code sets max := int(math.Inf(-1)) which converts negative infinity to int
(implementation-defined) — replace this by first checking the slice is non-empty
and initializing max from the first element (e.g., max := arr[0]) before
looping, or alternatively initialize max to math.MinInt (math.MinInt64 or
math.MinInt depending on types) to avoid converting from math.Inf.

// RemoveDuplicates returns a new slice with duplicate values removed,
// preserving the original order of elements.
func RemoveDuplicates(numbers []int) []int {
// TODO: Implement this function
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

Remove TODO comment.

The function is fully implemented—remove the placeholder TODO comment.

-	// TODO: Implement this function
 	hs := map[int]bool{}
📝 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
// TODO: Implement this function
hs := map[int]bool{}
🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 47,
remove the leftover "// TODO: Implement this function" comment because the
function is already implemented; simply delete that comment line so the source
no longer contains a misleading TODO placeholder.

// ReverseSlice returns a new slice with elements in reverse order.
func ReverseSlice(slice []int) []int {
result := []int{}
// TODO: Implement this function
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

Remove TODO comment.

The function is fully implemented—remove the placeholder TODO comment.

-	// TODO: Implement this function
 	for i := len(slice) - 1; i >= 0 ; 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
// TODO: Implement this function
for i := len(slice) - 1; i >= 0 ; i-- {
🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 63,
remove the leftover "// TODO: Implement this function" comment because the
function has been implemented; delete that single-line placeholder and ensure
the file is saved (run gofmt / go vet as needed) so no stray TODO remains.

@RezaSi RezaSi merged commit efaf9db into RezaSi:main Nov 8, 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