FastPrepRepair a Binary Search

Repair a Binary Search

Jefferies logoJefferies● EasyFULLTIMEOA
Learn

Problem statement

You are given an integer array values sorted in non-decreasing order and an integer target.

Return the last index at which target occurs. If the value is absent, return -1.

Your implementation must use binary search and must terminate for adjacent search boundaries and duplicate values.

Function

repairedBinarySearch(values: int[], target: int) → int

Examples

Example 1

values = [1,2,5,9,9]target = 5return = 2

The value 5 occurs at index 2.

Example 2

values = [1,2,5,9,9]target = 9return = 4

The target occurs at indices 3 and 4, so the last occurrence is index 4.

Example 3

values = []target = 7return = -1

An empty array contains no matching occurrence.

Constraints

  • 0 <= values.length <= 100000
  • -1000000000 <= values[i], target <= 1000000000
  • values is sorted in non-decreasing order.

More Jefferies problems

See Jefferies hiring insights
public int repairedBinarySearch(int[] values, int target) {
    // Write your code here.
}
values[1,2,5,9,9]
target5
expected2
Checking account…