Add solution for Challenge 22 by jeffreyyjp#1206
Add solution for Challenge 22 by jeffreyyjp#1206github-actions[bot] merged 3 commits intoRezaSi:mainfrom
Conversation
WalkthroughIntroduces a Go solution for the coin change problem with dynamic programming. Adds Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
cat -n challenge-22/submissions/jeffreyyjp/solution-template.goRepository: 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.
| 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.
|
🎉 Auto-merged! This PR was automatically merged after 2 days with all checks passing. Thank you for your contribution, @jeffreyyjp! |
Challenge 22 Solution
Submitted by: @jeffreyyjp
Challenge: Challenge 22
Description
This PR contains my solution for Challenge 22.
Changes
challenge-22/submissions/jeffreyyjp/solution-template.goTesting
Thank you for reviewing my submission! 🚀