Add solution for Challenge 21 by ForcemCS#1456
Conversation
WalkthroughA new Go solution file adds three public binary-search utility functions: BinarySearch (iterative), BinarySearchRecursive (recursive), and FindInsertPosition. Each function implements standard binary-search logic with appropriate edge-case handling for arrays of integers. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 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. Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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
🧹 Nitpick comments (2)
challenge-21/submissions/ForcemCS/solution-template.go (2)
28-44: Remove leftover TODO comment and consider formatting cleanup.The implementation is correct. However, the
TODOcomment on line 29 should be removed since the function is now implemented. Additionally, there are minor spacing inconsistencies (e.g.,0 , len(arr) -1vs standard0, len(arr)-1).✨ Suggested cleanup
func BinarySearch(arr []int, target int) int { - // TODO: Implement this function - left, right := 0 , len(arr) -1 - + left, right := 0, len(arr)-1 + for left <= right { - mid := left + (right - left ) / 2 - - if arr[mid] == target { - return mid - } else if arr[mid] < target { - left = mid + 1 - } else { - right = mid -1 - } + mid := left + (right-left)/2 + + if arr[mid] == target { + return mid + } else if arr[mid] < target { + left = mid + 1 + } else { + right = mid - 1 + } } return -1 }
48-54: Remove leftover TODO comment.The
TODOcomment on line 49 should be removed since the function is implemented.
| } else { | ||
| return BinarySearchRecursive(arr, target, left , right -1 ) |
There was a problem hiding this comment.
Bug: Use mid - 1 instead of right - 1 in the recursive call.
Line 61 passes right - 1 but should pass mid - 1 to correctly narrow the search space to the left half. This bug degrades performance from O(log n) to O(n) in worst cases and doesn't match the standard binary search algorithm.
🐛 Proposed fix
} else {
- return BinarySearchRecursive(arr, target, left , right -1 )
+ return BinarySearchRecursive(arr, target, left, mid-1)
}📝 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.
| } else { | |
| return BinarySearchRecursive(arr, target, left , right -1 ) | |
| } else { | |
| return BinarySearchRecursive(arr, target, left, mid-1) | |
| } |
Challenge 21 Solution
Submitted by: @ForcemCS
Challenge: Challenge 21
Description
This PR contains my solution for Challenge 21.
Changes
challenge-21/submissions/ForcemCS/solution-template.goTesting
Thank you for reviewing my submission! 🚀