Skip to content

Conversation

@AlexO-85
Copy link
Contributor

@AlexO-85 AlexO-85 commented Nov 6, 2025

Challenge 21 Solution

Submitted by: @AlexO-85
Challenge: Challenge 21

Description

This PR contains my solution for Challenge 21.

Changes

  • Added solution file to challenge-21/submissions/AlexO-85/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 6, 2025

Walkthrough

A 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

Cohort / File(s) Summary
Challenge-21 Binary Search Solution
challenge-21/submissions/AlexO-85/solution-template.go
Added Go solution with three public functions: BinarySearch (iterative binary search), BinarySearchRecursive (recursive binary search), and FindInsertPosition (insertion index locator). Includes test harness with sample outputs demonstrating each function on sorted integer slices.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Verify correctness of binary search logic in both iterative and recursive implementations
  • Check boundary conditions and edge cases (empty arrays, single elements, missing targets)
  • Confirm FindInsertPosition returns correct insertion indices for maintaining sorted order

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 clearly identifies the main change: adding a solution for Challenge 21 by a specific author.
Description check ✅ Passed The description is directly related to the changeset, explaining the Challenge 21 solution submission and the file 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: 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 = mid and left = mid (lines 60, 62) instead of the standard right = mid - 1 and left = 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 left and right, requiring callers to pass 0 and len(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 - 1 is 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

📥 Commits

Reviewing files that changed from the base of the PR and between 1a06951 and 4371f03.

📒 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.

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.

1 participant