-
-
Notifications
You must be signed in to change notification settings - Fork 736
Add solution for Challenge 21 by 4m4x #778
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
Conversation
WalkthroughA new Go file is added to the Challenge 21 submission directory containing three binary search implementations: a non-recursive BinarySearch function, a recursive BinarySearchRecursive function, and a FindInsertPosition function for determining insertion indices in sorted arrays. The file includes a main function with example usage and no external dependencies. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. 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 (2)
challenge-21/submissions/4m4x/solution-template.go (2)
26-48: Iterative binary search implementation looks correct; only minor style nitsThe search bounds and termination condition are correct and handle empty slices safely. The only small nits are:
- The early
len(arr) == 0check is redundant because thefor low <= highloop withhigh := len(arr)-1already covers the empty case.- The inline comment is in Russian; you might consider rewriting it in English so the entire team can easily read it.
Both are purely optional cleanups.
50-70: Recursive binary search is correct; consider tightening base cases and parameter styleLogic and bounds handling look good and are safe for empty slices. You can make it slightly clearer and more idiomatic by:
- Combining the empty-slice and
left > rightchecks into a single base case before computingmid.- Grouping parameter types for
leftandright.For example:
-func BinarySearchRecursive(arr []int, target int, left int, right int) int { - if len(arr) == 0 { - return -1 - } - - mid := (left + right) / 2 - - if left > right { - return -1 - } +func BinarySearchRecursive(arr []int, target int, left, right int) int { + if len(arr) == 0 || left > right { + return -1 + } + + mid := (left + right) / 2The rest of the function can stay as‑is. This keeps the exit conditions up front and avoids computing
midwhen you already know the search is done.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-21/submissions/4m4x/solution-template.go(1 hunks)
🔇 Additional comments (2)
challenge-21/submissions/4m4x/solution-template.go (2)
7-24: Main function is clear and useful for local validationThe sample data and prints exercise all three functions and make it easy to sanity‑check behavior; no changes needed here.
72-86: Insert-position finder matches lower_bound semantics and handles edge casesThe loop invariant and final
return leftgive the correct insertion index for all cases, including empty slices, targets smaller than all elements, and larger than all elements. Implementation is clean and idiomatic.
Challenge 21 Solution
Submitted by: @4m4x
Challenge: Challenge 21
Description
This PR contains my solution for Challenge 21.
Changes
challenge-21/submissions/4m4x/solution-template.goTesting
Thank you for reviewing my submission! 🚀