Skip to content

Conversation

@kiramux
Copy link
Contributor

@kiramux kiramux commented Nov 8, 2025

Challenge 19 Solution

Submitted by: @kiramux
Challenge: Challenge 19

Description

This PR contains my solution for Challenge 19.

Changes

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

Walkthrough

Adds a new Go program solution-template.go that implements four exported slice utilities—FindMax, RemoveDuplicates, ReverseSlice, FilterEven—and a main function demonstrating their usage with a sample slice.

Changes

Cohort / File(s) Summary
Slice utility implementation
challenge-19/submissions/kiramux/solution-template.go
New file adding four exported functions: FindMax(numbers []int) int (returns max or 0 for empty slice), RemoveDuplicates(numbers []int) []int (preserves original order, uses a map for seen values), ReverseSlice(slice []int) []int (returns a new reversed slice), and FilterEven(numbers []int) []int (returns only even numbers). Includes a main demonstrating each function.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Main as main()
    participant FindMax as FindMax()
    participant RemoveDup as RemoveDuplicates()
    participant Reverse as ReverseSlice()
    participant Filter as FilterEven()

    Main->>FindMax: pass sample slice
    FindMax-->>Main: max value
    Main->>RemoveDup: pass sample slice
    RemoveDup-->>Main: deduped slice
    Main->>Reverse: pass sample slice
    Reverse-->>Main: reversed slice
    Main->>Filter: pass sample slice
    Filter-->>Main: filtered evens
    Note over Main: prints results
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Quick checks: correctness of empty-slice handling in FindMax, preservation of original order in RemoveDuplicates, and that reverse/filter return new slices (no unintended in-place modifications).

Possibly related PRs

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically identifies that this PR adds a solution for Challenge 19 submitted by kiramux, which directly matches the changeset.
Description check ✅ Passed The description is clearly related to the changeset, providing context about the Challenge 19 solution submission, the file location, and testing status.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.

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

44-56: Consider pre-allocating the result slice for better performance.

While the logic is correct and the use of map[int]struct{} is idiomatic, pre-allocating the result slice can reduce memory allocations during append operations.

Apply this diff to pre-allocate the result slice:

 func RemoveDuplicates(numbers []int) []int {
 	seen := make(map[int]struct{}, len(numbers))
-	res := []int{}
+	res := make([]int, 0, len(numbers))
 	for _, v := range numbers {
 		if _, ok := seen[v]; !ok {
 			seen[v] = struct{}{}
 			res = append(res, v)
 		}
 	}
 	return res
 }

58-73: Consider building the reversed slice directly instead of copy-then-swap.

The current implementation copies the slice and then reverses it in-place. You can achieve the same result more efficiently by building the reversed slice directly in a single pass.

Apply this diff to build the reversed slice directly:

 func ReverseSlice(slice []int) []int {
 	if len(slice) == 0 {
 		return []int{}
 	}
 	result := make([]int, len(slice))
-	copy(result, slice)
-	left := 0
-	right := len(result) - 1
-	for left < right {
-		result[left], result[right] = result[right], result[left]
-		left++
-		right--
+	for i, v := range slice {
+		result[len(slice)-1-i] = v
 	}
 	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 2d4b9cf and 1259272.

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

8-27: LGTM! Clear demonstration of all utility functions.

The main function effectively demonstrates each utility function with appropriate test data and clear output labels.


29-42: Consider the ambiguity of returning 0 for empty slices.

Returning 0 for an empty slice is ambiguous since 0 could be a valid maximum value (e.g., in a slice of negative numbers). This makes it impossible to distinguish between "no elements" and "maximum is 0". Consider returning an error or using a tuple return (int, bool) to indicate validity.

Example demonstrating the ambiguity:

FindMax([]int{})        // returns 0 - empty slice
FindMax([]int{-5, 0})   // returns 0 - valid maximum

75-85: LGTM! Clean and correct even number filter.

The logic is straightforward and correct. Pre-allocating the result slice here would be wasteful since the final size is unknown, and the current approach is appropriate.

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

44-56: LGTM! Excellent use of empty struct for the seen map.

The logic is correct and order-preserving. Consider pre-allocating the result slice capacity for a minor performance improvement:

-	res := []int{}
+	res := make([]int, 0, len(numbers))

70-80: LGTM! Correct even number filtering.

The modulo operation correctly identifies even numbers. Optionally, pre-allocate capacity for better performance:

-	result := []int{}
+	result := make([]int, 0, len(numbers))
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1259272 and 58b13e6.

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

3-6: LGTM! Clean import usage.

Both imports are necessary and properly used throughout the code.


8-27: LGTM! Well-structured demonstration.

The main function provides clear demonstrations of all four utilities with labeled output.


29-42: LGTM! Correct implementation with proper edge case handling.

The use of math.MinInt as the initial value is the right approach, ensuring correct behavior even with all-negative slices.


58-68: LGTM! Clean reverse implementation.

The logic correctly reverses the slice with proper pre-allocation and empty slice handling.

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