Repair a Binary Search
Updated
Problem statement
You are given an integer array values sorted from smallest to largest 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 finish correctly even when the search boundaries are next to each other or values repeat.
Function
repairedBinarySearch(values: int[], target: int) → intExamples
Example 1
values = [1,2,5,9,9]target = 5return = 2The value 5 occurs at index 2.
Example 2
values = [1,2,5,9,9]target = 9return = 4The target occurs at indices 3 and 4, so the last occurrence is index 4.
Example 3
values = []target = 7return = -1An empty array contains no matching occurrence.
Constraints
0 <= values.length <= 100000-1000000000 <= values[i], target <= 1000000000valuesis sorted in non-decreasing order.
More Jefferies Online Assessment questions
Similar questions at other companies
- First Missing Number in a Consecutive ArrayArista Networks · OA · PHONE SCREENArista Networks interview questions
- Floor Square Roots for an ArrayWells Fargo · OAWells Fargo interview questions
- Find the First Matching Ordered EntryFigma · OAFigma interview questions
- Minimum BeautyBloomberg LP · OABloomberg LP interview questions
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Count Pairs of Overlapping SquaresCapital One · OACapital One interview questions
- Minimum Operations to Make a SubsequenceGoogle · OAGoogle interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Sliding Window MedianAuxoAI · OA