Implement Binary Search
Updated
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) → intExamples
Example 1
nums = [-4,-1,0,3,9]target = 3return = 3The target is at index 3.
Example 2
nums = [1,4,7,11]target = 5return = -1The target does not occur.
Constraints
0 <= nums.length <= 100000-10^9 <= nums[i], target <= 10^9numsis sorted in nondecreasing order.
More Qualys Onsite Interview questions
- Delete a Doubly Linked List Node at a PositionONSITE INTERVIEW · Seen Oct 2026
- Find the Entry of a Linked List CycleONSITE INTERVIEW · Seen Oct 2026
- Insert a Doubly Linked List Node at a PositionONSITE INTERVIEW · Seen Oct 2026
- Rotate an Array Right by K StepsONSITE INTERVIEW · Seen Oct 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Roll a Weighted DieLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Messages in an Inclusive Timestamp RangeAttentive · PHONE SCREEN · ONSITE INTERVIEWAttentive interview questions
- Count Occurrences in a Sorted ArrayAdobe · PHONE SCREEN · ONSITE INTERVIEWAdobe interview questions
- Closest Version DateAmazon · ONSITE INTERVIEW
- Count Points in Each Boundary IntervalBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Recursive Binary SearchEPAM · ONSITE INTERVIEWEPAM interview questions
- Find Unmet Users With Shared InterestsPalantir · ONSITE INTERVIEWPalantir interview questions
- Find Maximum Frequency of NumberGoogle · ONSITE INTERVIEWGoogle interview questions
- Song Duration Pairs Divisible by 60The Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Search a Sorted Array of WordsExpedia · ONSITE INTERVIEWExpedia interview questions