Implement Binary Search From Scratch
Reported by candidates from Cerebras's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
This Cerebras OA, reported in September 2025, boils down to one thing: write binary search without a library call and don't fumble the boundaries. It's the plainest binary-search question you'll see, which is exactly why people lose points on it. Off-by-one errors, empty arrays, and integer overflow habits are where it goes wrong. You return the index of any occurrence of the target, or -1 if it's missing. If your mind goes blank on the loop condition during the live assessment, StealthCoder is the safety net that can put a clean version on screen without the proctor seeing it.
The problem
Given an integer array sorted in nondecreasing order and a target, implement iterative binary search from scratch. Return the index of any occurrence of the target, or -1 when it is absent. Function binarySearch(nums: int[], target: int) → int Examples Example 1 nums = [-4,-1,0,3,9] target = 3 return = 3 The target is at index 3. Example 2 nums = [1,4,7,11] target = 5 return = -1 The target does not occur. Constraints 0 <= nums.length <= 100000 -10^9 <= nums[i], target <= 10^9 nums is sorted in nondecreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is keeping an invariant. Pick one convention and stick with it: lo = 0, hi = n - 1, loop while lo <= hi, compute mid = lo + (hi - lo) / 2, then compare nums[mid] to target. Equal means return mid. Smaller means lo = mid + 1. Larger means hi = mid - 1. Every step must shrink the range, or you get an infinite loop. The common pitfall is mixing conventions, like using hi = n with lo <= hi, which reads past the end. Empty input (length 0) falls out naturally, since hi = -1 skips the loop and you return -1. Duplicates don't matter because any matching index is valid. Time is O(log n), space O(1). The constraints are small enough that overflow won't bite in Python, but the lo + (hi - lo) / 2 habit is safe in Java or C++. If you freeze on the live OA, StealthCoder is your hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Implement Binary Search From Scratch cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as binary search. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Cerebras's OA.
Cerebras reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Implement Binary Search From Scratch FAQ
How hard is the Cerebras binary search question really?+
It's easy on paper. The difficulty is precision, not insight. You need a correct loop condition, correct pointer updates, and the -1 return. Most failures come from off-by-one bugs, so trace the two examples by hand before submitting.
What's the trick to avoid infinite loops?+
Always move past mid. Use lo = mid + 1 and hi = mid - 1 with a lo <= hi loop. If you ever set lo = mid or hi = mid inside that style of loop, the range may stop shrinking and the code hangs on some inputs.
Do I need to handle duplicates?+
No. The problem says return the index of any occurrence. So the first time nums[mid] equals target, return mid. Don't spend time on leftmost or rightmost variants unless the prompt changes.
What edge cases should I test?+
Test an empty array, a single element that matches, a single element that doesn't, a target smaller than everything, a target larger than everything, and a target missing between two values like Example 2. Those cover almost every boundary bug.
How do I prepare in 48 hours?+
Write iterative binary search from memory three times, in your OA language, without looking. Then trace it on Example 1 and Example 2 by hand. Once the template is automatic, the problem takes a few minutes and the rest of your time goes to harder questions.