Skip to content

Conversation

@yz4230
Copy link
Contributor

@yz4230 yz4230 commented Nov 16, 2025

Challenge 21 Solution

Submitted by: @yz4230
Challenge: Challenge 21

Description

This PR contains my solution for Challenge 21.

Changes

  • Added solution file to challenge-21/submissions/yz4230/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 16, 2025

Walkthrough

A new Go submission file implementing three binary search utility functions: an iterative binary search, a recursive variant, and a function to find insertion position in a sorted array. The file includes a main function demonstrating usage of these utilities.

Changes

Cohort / File(s) Change Summary
Binary Search Utilities
challenge-21/submissions/yz4230/solution-template.go
New file with iterative BinarySearch, recursive BinarySearchRecursive, and FindInsertPosition functions, plus a sample main demonstrating their usage.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • All three functions implement standard binary search algorithms with straightforward logic
  • Single file addition with self-contained functionality
  • Consistent pattern throughout (no heterogeneous or complex logic density)

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a solution file for Challenge 21 by a specific author, which matches the PR objectives and file additions.
Description check ✅ Passed The description is directly related to the changeset, clearly explaining that it adds a Challenge 21 solution and specifying the file path where the solution was added.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
challenge-21/submissions/yz4230/solution-template.go (2)

7-24: Consider adding more comprehensive test cases.

The main function demonstrates basic usage well, but for a practice submission, consider testing additional scenarios:

  • Target not found (for both search functions)
  • Empty array
  • Single element array
  • Target at boundaries (first/last element)
  • Target outside array range (smaller/larger than all elements)

64-78: Significant code duplication with BinarySearch.

Lines 67-75 duplicate the search logic from BinarySearch (lines 31-39). The only difference is the final return value (left vs -1).

Consider refactoring to eliminate duplication, for example by having BinarySearch use FindInsertPosition internally:

func BinarySearch(arr []int, target int) int {
    pos := FindInsertPosition(arr, target)
    if pos < len(arr) && arr[pos] == target {
        return pos
    }
    return -1
}

Or by extracting the common binary search logic into a helper function.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9b711a4 and 95fc7aa.

📒 Files selected for processing (1)
  • challenge-21/submissions/yz4230/solution-template.go (1 hunks)
🔇 Additional comments (1)
challenge-21/submissions/yz4230/solution-template.go (1)

46-60: LGTM! Correct recursive implementation.

The recursive binary search correctly implements a closed interval [left, right] approach with proper base case handling and boundary adjustments.

Comment on lines +28 to +42
func BinarySearch(arr []int, target int) int {
left, right := 0, len(arr)
for left < right {
mid := (right - left) / 2 + left
midv := arr[mid]
if target < midv {
right = mid
} else if midv < target {
left = mid + 1
} else {
return mid
}
}
return -1
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Inconsistent interval convention with recursive variant.

The iterative BinarySearch uses a half-open interval [left, right) (initialized with right := len(arr)), while BinarySearchRecursive uses a closed interval [left, right] (called with len(arr)-1). Both implementations are correct individually, but the inconsistency can confuse maintainers.

Consider standardizing on one convention across both functions for better code consistency.

🤖 Prompt for AI Agents
challenge-21/submissions/yz4230/solution-template.go lines 28-42: the iterative
BinarySearch uses a half-open interval [left,right) (right := len(arr)) while
the recursive variant uses a closed interval [left,right] (invoked with
len(arr)-1), causing inconsistency; pick one convention and make both functions
match — either change the iterative function to use closed interval (initialize
right := len(arr)-1, adjust loop condition to left <= right and update pointers
accordingly) or change the recursive variant to use half-open bounds (accept
right as len(arr) and treat right as exclusive in the recursion), and update all
comparisons/indices and base cases to follow the chosen convention consistently.

@RezaSi RezaSi merged commit 9ee9079 into RezaSi:main Nov 18, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants