Skip to content

Add solution for Challenge 21 by ForcemCS#1456

Merged
RezaSi merged 1 commit intoRezaSi:mainfrom
ForcemCS:challenge-21-ForcemCS
Mar 16, 2026
Merged

Add solution for Challenge 21 by ForcemCS#1456
RezaSi merged 1 commit intoRezaSi:mainfrom
ForcemCS:challenge-21-ForcemCS

Conversation

@ForcemCS
Copy link
Contributor

Challenge 21 Solution

Submitted by: @ForcemCS
Challenge: Challenge 21

Description

This PR contains my solution for Challenge 21.

Changes

  • Added solution file to challenge-21/submissions/ForcemCS/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 Feb 27, 2026

Walkthrough

A new Go solution file adds three public binary-search utility functions: BinarySearch (iterative), BinarySearchRecursive (recursive), and FindInsertPosition. Each function implements standard binary-search logic with appropriate edge-case handling for arrays of integers.

Changes

Cohort / File(s) Summary
Binary Search Utilities
challenge-21/submissions/ForcemCS/solution-template.go
Added three public functions for binary-search operations: BinarySearch (iterative, returns target index or -1), BinarySearchRecursive (recursive variant with left/right parameters), and FindInsertPosition (returns insertion index to maintain sorted order). Includes example usage in main function.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 Hops through sorted arrays with binary grace,
Left and right pointers race,
Finding targets, positions too,
Recursive loops and loops that flew,
Search and sort—the rabbit's way! 🔍

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a solution for Challenge 21 by the user ForcemCS, which directly corresponds to the new Go solution file added in the changeset.
Description check ✅ Passed The description is directly related to the changeset, explaining that it contains a Challenge 21 solution submission with details about the added file and testing performed.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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/ForcemCS/solution-template.go (2)

28-44: Remove leftover TODO comment and consider formatting cleanup.

The implementation is correct. However, the TODO comment on line 29 should be removed since the function is now implemented. Additionally, there are minor spacing inconsistencies (e.g., 0 , len(arr) -1 vs standard 0, len(arr)-1).

✨ Suggested cleanup
 func BinarySearch(arr []int, target int) int {
-	// TODO: Implement this function
-	left, right := 0 , len(arr) -1 
-	
+	left, right := 0, len(arr)-1
+
 	for left <= right {
-	    mid := left + (right - left ) / 2
-	    
-	    if arr[mid]  == target {
-	        return mid
-	    } else if  arr[mid] < target {
-	        left = mid + 1 
-	    } else {
-	        right = mid -1 
-	    }
+		mid := left + (right-left)/2
+
+		if arr[mid] == target {
+			return mid
+		} else if arr[mid] < target {
+			left = mid + 1
+		} else {
+			right = mid - 1
+		}
 	}
 	return -1
 }

48-54: Remove leftover TODO comment.

The TODO comment on line 49 should be removed since the function is implemented.


ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a7ac764 and 3ccc676.

📒 Files selected for processing (1)
  • challenge-21/submissions/ForcemCS/solution-template.go

Comment on lines +60 to +61
} else {
return BinarySearchRecursive(arr, target, left , right -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 | 🟠 Major

Bug: Use mid - 1 instead of right - 1 in the recursive call.

Line 61 passes right - 1 but should pass mid - 1 to correctly narrow the search space to the left half. This bug degrades performance from O(log n) to O(n) in worst cases and doesn't match the standard binary search algorithm.

🐛 Proposed fix
 	} else {
-	    return BinarySearchRecursive(arr, target, left , right -1 )
+	    return BinarySearchRecursive(arr, target, left, mid-1)
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} else {
return BinarySearchRecursive(arr, target, left , right -1 )
} else {
return BinarySearchRecursive(arr, target, left, mid-1)
}

@RezaSi RezaSi merged commit 6d272e7 into RezaSi:main Mar 16, 2026
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