FastPrepImplement Binary Search From Scratch

Implement Binary Search From Scratch

Cerebras logoCerebras● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

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 = 3return = 3

The target is at index 3.

Example 2

nums = [1,4,7,11]target = 5return = -1

The target does not occur.

Constraints

  • 0 <= nums.length <= 100000
  • -10^9 <= nums[i], target <= 10^9
  • nums is sorted in nondecreasing order.

More Cerebras problems

See Cerebras hiring insights
public int binarySearch(int[] nums, int target) {
  // Write your iterative binary search here.
}
nums[-4,-1,0,3,9]
target3
expected3
Checking account…