-
-
Notifications
You must be signed in to change notification settings - Fork 699
Add solution for Challenge 21 by AlexO-85 #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA Go solution template for challenge-21 is added, implementing three binary search functions: a non-recursive variant, a recursive variant, and a function to find insertion positions in sorted arrays, with a test harness demonstrating their usage. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
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.
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 (4)
challenge-21/submissions/AlexO-85/solution-template.go (4)
49-70: Consider using standard binary search algorithm.The implementation uses an unconventional approach with
right = midandleft = mid(lines 60, 62) instead of the standardright = mid - 1andleft = mid + 1. While this appears to work due to the convergence check at lines 54-56, it's more complex and less idiomatic than the standard algorithm.Standard binary search would be:
func BinarySearch(arr []int, target int) int { left := 0 right := len(arr) - 1 - if right == -1 || target < arr[left] || target > arr[right] { + if right == -1 { return -1 } - if arr[left] == target { - return left - } - - if arr[right] == target { - return right - } - for left <= right { mid := left + (right-left)/2 - // fmt.Printf("Looking between %d and %d for %d, mid: %d\n", left, right, target, mid) - - if mid == left || mid == right { - return -1 - } - - switch { - case target < arr[mid]: - right = mid - case target > arr[mid]: - left = mid - default: + if arr[mid] == target { return mid + } else if arr[mid] < target { + left = mid + 1 + } else { + right = mid - 1 } - } return -1 }
52-52: Remove commented debug code.The commented-out debug statement should be removed before merging.
- // fmt.Printf("Looking between %d and %d for %d, mid: %d\n", left, right, target, mid)
74-104: Refactor to hide internal recursion parameters.The public API exposes internal recursion parameters
leftandright, requiring callers to pass0andlen(arr)-1. Consider a cleaner API with an internal helper.-func BinarySearchRecursive(arr []int, target int, left int, right int) int { +func BinarySearchRecursive(arr []int, target int) int { + return binarySearchRecursiveHelper(arr, target, 0, len(arr)-1) +} - if right == -1 || left > right || target < arr[left] || target > arr[right] { +func binarySearchRecursiveHelper(arr []int, target int, left int, right int) int { + if left > right { return -1 } - if arr[left] == target { - return left - } - - if arr[right] == target { - return right - } - mid := left + (right-left)/2 - if mid == left || mid == right { - return -1 - } - - switch { - case target < arr[mid]: - right = mid - case target > arr[mid]: - left = mid - default: + if arr[mid] == target { return mid + } else if arr[mid] < target { + return binarySearchRecursiveHelper(arr, target, mid+1, right) + } else { + return binarySearchRecursiveHelper(arr, target, left, mid-1) } - - return BinarySearchRecursive(arr, target, left, right) }Also update the caller in main:
- recursiveIndex := BinarySearchRecursive(arr, target, 0, len(arr)-1) + recursiveIndex := BinarySearchRecursive(arr, target)
108-148: Simplify FindInsertPosition logic.The insertion position logic at lines 127-133 is complex and difficult to verify. The else branch returning
right - 1is particularly unclear. Consider using the standard approach for finding insertion positions.func FindInsertPosition(arr []int, target int) int { left := 0 - right := len(arr) - 1 - - if right == -1 { - return 0 - } - - if target < arr[left] { - return left - } - - if target > arr[right] { - return right + 1 - } + right := len(arr) for left < right { mid := left + (right-left)/2 - - if mid == left || mid == right { - if arr[mid] < target { - return left + 1 - } else { - return right - 1 - } - } - - switch { - case target < arr[mid]: - right = mid - case target > arr[mid]: + if arr[mid] < target { left = mid - default: - return mid + } else { + right = mid } - } - return 0 - + return left }This standard approach is simpler, more maintainable, and uses exclusive upper bound which makes the logic clearer.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-21/submissions/AlexO-85/solution-template.go(1 hunks)
🔇 Additional comments (1)
challenge-21/submissions/AlexO-85/solution-template.go (1)
7-29: Test harness looks good.The main function provides a reasonable test harness for the three binary search implementations. Consider adding more edge cases like searching for non-existent elements in multi-element arrays, empty arrays, and targets at various boundary positions.
Challenge 21 Solution
Submitted by: @AlexO-85
Challenge: Challenge 21
Description
This PR contains my solution for Challenge 21.
Changes
challenge-21/submissions/AlexO-85/solution-template.goTesting
Thank you for reviewing my submission! 🚀