Skip to content

Conversation

@AlexO-85
Copy link
Contributor

@AlexO-85 AlexO-85 commented Nov 6, 2025

Challenge 22 Solution

Submitted by: @AlexO-85
Challenge: Challenge 22

Description

This PR contains my solution for Challenge 22.

Changes

  • Added solution file to challenge-22/submissions/AlexO-85/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 6, 2025

Walkthrough

Introduces a new Go solution implementing a greedy coin-change algorithm under challenge-22/submissions/AlexO-85/. The code defines two exported functions—MinCoins and CoinCombination—that compute minimum coin count and denomination breakdown respectively. Includes a main routine demonstrating both functions with predefined denominations and test amounts.

Changes

Cohort / File(s) Summary
New solution file
challenge-22/submissions/AlexO-85/solution-template.go
Adds MinCoins and CoinCombination functions implementing greedy coin-change logic, plus a main entry point demonstrating the algorithm with denominations [1, 5, 10, 25, 50] and sample amounts. Both functions sort denominations in descending order and handle unmakeable amounts via sentinel values (-1 or empty map).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Straightforward greedy algorithm with no complex control flow
  • Single new file with self-contained logic
  • Primary review focus: correctness of greedy subtraction loop, denomination sorting order, and edge case handling for unmakeable amounts

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% 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 title clearly and concisely summarizes the main change: adding a solution for Challenge 22, matching the changeset which introduces a new solution file.
Description check ✅ Passed The description directly relates to the changeset, explaining that it adds a solution file for Challenge 22 and noting testing and best practices compliance.

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: 2

🧹 Nitpick comments (2)
challenge-22/submissions/AlexO-85/solution-template.go (2)

79-84: Simplify map initialization.

Go initializes missing int values in maps to 0, so the existence check is unnecessary.

-			_, ok := combination[val]
-			if !ok {
-				combination[val] = 0
-			}
-			combination[val]++
+			combination[val]++

39-48: Consider using division instead of repeated subtraction for efficiency.

The inner loop repeatedly subtracts val from amount. Using integer division and modulo is more efficient and cleaner.

-		for amount > 0 {
-
-			if val > amount {
-				break
-			}
-
-			count++
-			amount -= val
-		}
+		if val <= amount {
+			numCoins := amount / val
+			count += numCoins
+			amount = amount % val
+		}

Apply a similar change to CoinCombination (lines 73-85):

-		for amount > 0 {
-
-			if val > amount {
-				break
-			}
-
-			combination[val]++
-			amount -= val
-		}
+		if val <= amount {
+			combination[val] = amount / val
+			amount = amount % val
+		}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a06951 and eca9dbc.

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

8-28: LGTM! Clear demonstration code.

The main function provides a good demonstration of both functions with standard US coin denominations.


32-60: Incorrect review comment. The challenge explicitly requires a greedy implementation and acknowledges its limitations in the README. The problem statement specifies "Your solution should implement the greedy approach, which always chooses the largest coin possible" and notes "The greedy approach for coin change works optimally for the standard U.S. coin denominations". The solution correctly implements the greedy algorithm as required. All test cases are designed for the greedy approach, including non-canonical coin sets where greedy is expected to be used, not an optimal solution.

Likely an incorrect or invalid review comment.

Comment on lines +34 to +35
slices.Sort(denominations)
slices.Reverse(denominations)
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 | 🟠 Major

Fix side effect: input slice is modified.

slices.Sort and slices.Reverse modify the input slice in place, which means the caller's denominations slice will be reordered. This is an unexpected side effect.

Copy the slice before sorting to avoid modifying the caller's data:

+	// Make a copy to avoid modifying the input slice
+	denoms := make([]int, len(denominations))
+	copy(denoms, denominations)
+	
-	slices.Sort(denominations)
-	slices.Reverse(denominations)
+	slices.Sort(denoms)
+	slices.Reverse(denoms)

-	for _, val := range denominations {
+	for _, val := range denoms {

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

🤖 Prompt for AI Agents
In challenge-22/submissions/AlexO-85/solution-template.go around lines 34 to 35,
slices.Sort and slices.Reverse are mutating the input denominations slice
causing an unexpected side effect; to fix this, make a copy of the denominations
slice (e.g. allocate a new slice and copy values) and run Sort/Reverse on that
copy so the original caller slice remains unchanged, then use the sorted copy
for further processing.

Comment on lines +69 to +70
slices.Sort(denominations)
slices.Reverse(denominations)
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 | 🟠 Major

Fix side effect: input slice is modified.

Same issue as in MinCoins—the input slice is modified in place.

Apply the same fix to copy the slice before sorting:

+	// Make a copy to avoid modifying the input slice
+	denoms := make([]int, len(denominations))
+	copy(denoms, denominations)
+	
-	slices.Sort(denominations)
-	slices.Reverse(denominations)
+	slices.Sort(denoms)
+	slices.Reverse(denoms)

-	for _, val := range denominations {
+	for _, val := range denoms {

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

🤖 Prompt for AI Agents
In challenge-22/submissions/AlexO-85/solution-template.go around lines 69 to 70,
the code sorts and reverses the denominations slice in place which mutates the
input; make a copy of the denominations slice first (e.g., use slices.Clone or
allocate a new slice and copy) and perform Sort and Reverse on that copy so the
original input is not modified.

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