Binary Search
Updated
Problem statement
The numbers in nums go up from left to right, and no number appears twice. Find target: give its position, counting from 0, or -1 when it is missing.
Here n is the length of nums. Try to look at only about log2(n) of the numbers. A plain left-to-right scan is also fast enough for these limits.
Function
search(nums: int[], target: int) → intExamples
Example 1
nums = [-40,-12,3,18,26,51,77,90,104]target = 104return = 8104 is the last of the nine values, so its position is 8.
Example 2
nums = [-40,-12,3,18,26]target = 20return = -120 is bigger than 18 and smaller than 26, and no value in between equals it. It is absent, so return -1.
Example 3
nums = [6,9]target = 6return = 0The first value, at position 0, is the one we want.
Constraints
1 <= nums.length <= 10^4-10^4 < nums[i], target < 10^4- Each value of
numsis larger than the one before it.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Roll a Weighted DieLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Implement Binary SearchQualys · ONSITE INTERVIEWQualys 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
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences