Skip to content

Add solution for Challenge 22 by jeffreyyjp#1206

Merged
github-actions[bot] merged 3 commits intoRezaSi:mainfrom
jeffreyyjp:challenge-22-jeffreyyjp
Apr 14, 2026
Merged

Add solution for Challenge 22 by jeffreyyjp#1206
github-actions[bot] merged 3 commits intoRezaSi:mainfrom
jeffreyyjp:challenge-22-jeffreyyjp

Conversation

@jeffreyyjp
Copy link
Copy Markdown
Contributor

Challenge 22 Solution

Submitted by: @jeffreyyjp
Challenge: Challenge 22

Description

This PR contains my solution for Challenge 22.

Changes

  • Added solution file to challenge-22/submissions/jeffreyyjp/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jan 25, 2026

Walkthrough

Introduces a Go solution for the coin change problem with dynamic programming. Adds MinCoins and CoinCombination functions to compute the minimum number of coins needed for given amounts and determine the actual coin composition. Includes a main program that tests these functions with US coin denominations and sample amounts.

Changes

Cohort / File(s) Summary
Coin Change Solution
challenge-22/submissions/jeffreyyjp/solution-template.go
Added MinCoins(amount, denominations) using DP to find minimum coin count (returns -1 if unreachable). Added CoinCombination(amount, denominations) using DP to track and return the actual coin composition as a map (returns empty map if unreachable). Both functions initialize DP arrays with amount+1 as a sentinel value, iterate over amounts and denominations, and propagate optimal subsolutions. Includes main program testing with predefined coin sets and amounts.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #654: Implements MinCoins and CoinCombination functions for Challenge 22 using a greedy approach instead of dynamic programming.
  • #1104: Implements the same exported functions for Challenge 22 with an alternative greedy algorithm.
  • #625: Adds the same MinCoins and CoinCombination functions to Challenge 22 solution file with a different algorithmic approach.
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding a solution for Challenge 22 by a specific contributor.
Description check ✅ Passed The description is directly related to the changeset, explaining the Challenge 22 solution submission and the files added.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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
Copy Markdown
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

🤖 Fix all issues with AI agents
In `@challenge-22/submissions/jeffreyyjp/solution-template.go`:
- Around line 58-76: Add input validation to prevent panics: in CoinCombination,
check if amount <= 0 (treat negative as invalid) and return an empty map
immediately; also validate the denominations slice contains only positive
integers (if any coin <= 0, return an empty map) before allocating
dp/combinations. Apply the same guards to the MinCoins function (check amount <=
0 and reject non‑positive denominations) so both functions consistently return a
safe empty result for invalid inputs.

Comment on lines +58 to +76
func CoinCombination(amount int, denominations []int) map[int]int {
// TODO: Implement this function
if amount == 0 || len(denominations) == 0 {
return make(map[int]int)
}

max := amount + 1
dp := make([]int, max)
combinations := make([]map[int]int, max)
for i := 1; i < len(dp); i++ {
dp[i] = max
combinations[i] = make(map[int]int)
}
combinations[0] = make(map[int]int)

for i := 1; i <= amount; i++ {
for _, coin := range denominations {
if i >= coin && dp[i] > dp[i-coin] + 1 {
dp[i] = dp[i-coin] + 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cat -n challenge-22/submissions/jeffreyyjp/solution-template.go

Repository: RezaSi/go-interview-practice

Length of output: 3059


Guard against negative or zero amounts and non‑positive denominations to avoid panics.

If amount < 0, then max := amount + 1 produces a negative or zero value, causing make([]int, max) to panic at runtime. Additionally, coin <= 0 can lead to invalid indexing logic. The current check only covers amount == 0, not negative values. Add explicit validation for both cases before processing.

🛡️ Proposed input validation
 func CoinCombination(amount int, denominations []int) map[int]int {
-    if amount == 0 || len(denominations) == 0 {
+    if amount <= 0 || len(denominations) == 0 {
         return make(map[int]int)
     }
+    for _, coin := range denominations {
+        if coin <= 0 {
+            return make(map[int]int)
+        }
+    }

Also add the same guards to MinCoins for consistency.

📝 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
func CoinCombination(amount int, denominations []int) map[int]int {
// TODO: Implement this function
if amount == 0 || len(denominations) == 0 {
return make(map[int]int)
}
max := amount + 1
dp := make([]int, max)
combinations := make([]map[int]int, max)
for i := 1; i < len(dp); i++ {
dp[i] = max
combinations[i] = make(map[int]int)
}
combinations[0] = make(map[int]int)
for i := 1; i <= amount; i++ {
for _, coin := range denominations {
if i >= coin && dp[i] > dp[i-coin] + 1 {
dp[i] = dp[i-coin] + 1
func CoinCombination(amount int, denominations []int) map[int]int {
// TODO: Implement this function
if amount <= 0 || len(denominations) == 0 {
return make(map[int]int)
}
for _, coin := range denominations {
if coin <= 0 {
return make(map[int]int)
}
}
max := amount + 1
dp := make([]int, max)
combinations := make([]map[int]int, max)
for i := 1; i < len(dp); i++ {
dp[i] = max
combinations[i] = make(map[int]int)
}
combinations[0] = make(map[int]int)
for i := 1; i <= amount; i++ {
for _, coin := range denominations {
if i >= coin && dp[i] > dp[i-coin] + 1 {
dp[i] = dp[i-coin] + 1
🤖 Prompt for AI Agents
In `@challenge-22/submissions/jeffreyyjp/solution-template.go` around lines 58 -
76, Add input validation to prevent panics: in CoinCombination, check if amount
<= 0 (treat negative as invalid) and return an empty map immediately; also
validate the denominations slice contains only positive integers (if any coin <=
0, return an empty map) before allocating dp/combinations. Apply the same guards
to the MinCoins function (check amount <= 0 and reject non‑positive
denominations) so both functions consistently return a safe empty result for
invalid inputs.

@github-actions github-actions bot merged commit b409553 into RezaSi:main Apr 14, 2026
6 checks passed
@github-actions
Copy link
Copy Markdown

🎉 Auto-merged!

This PR was automatically merged after 2 days with all checks passing.

Thank you for your contribution, @jeffreyyjp!

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