-
-
Notifications
You must be signed in to change notification settings - Fork 713
Add solution for Challenge 21 by yz4230 #764
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
WalkthroughThis pull request adds a new Go file containing binary search implementations for challenge-21. The file includes iterative binary search, recursive binary search, and a function to find insertion positions in sorted arrays, along with a demonstration main function that tests these implementations. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 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 (1)
challenge-21/submissions/yz4230/solution-template.go (1)
62-78:FindInsertPositionis correct; consider reducing duplication withBinarySearchThe insertion-index logic over
[left, right)is correct for all targets (including before-first, after-last, and duplicates), and returninglefton miss preserves sorted order. You could optionally factor the shared binary-search loop into a helper (e.g., a lower-bound function) and implementBinarySearchin terms of it to avoid duplicating nearly identical loops.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-21/submissions/yz4230/solution-template.go(1 hunks)
🔇 Additional comments (3)
challenge-21/submissions/yz4230/solution-template.go (3)
7-24: Samplemaincorrectly exercises all search helpersThe demo covers both search variants and the insert-position helper with representative inputs; no functional issues here.
26-42: IterativeBinarySearchis correct and handles edge cases wellLogic over a half-open interval
[left, right)with a safe mid calculation is sound, including for empty slices and out-of-range targets; it returns-1exactly when not found.
44-60: RecursiveBinarySearchRecursiveimplementation is logically soundClosed-interval
[left, right]handling with theleft > rightbase case avoids out-of-bounds access and correctly terminates for all cases (including empty slices when called withright = len(arr)-1).
Challenge 21 Solution
Submitted by: @yz4230
Challenge: Challenge 21
Description
This PR contains my solution for Challenge 21.
Changes
challenge-21/submissions/yz4230/solution-template.goTesting
Thank you for reviewing my submission! 🚀