First One in a Sorted Binary Array
Updated
Problem statement
The integer array nums contains only zeros and ones, sorted in nondecreasing order. Return the index of its first one.
For this exercise, assume indices start at zero and return -1 when the array contains no one, including when it is empty.
Find the answer in logarithmic time.
Function
firstOne(nums: int[]) → intExamples
Example 1
nums = [0,0,1,1]return = 2Indices 0 and 1 contain zero, and index 2 contains the first one.
Example 2
nums = [0,0,0]return = -1There is no one in the array, so return -1.
Example 3
nums = [1,1]return = 0The first element is already one, at index 0.
Constraints
0 <= nums.length <= 100000 <= nums[i] <= 1- nums is sorted in nondecreasing order.
More JP Morgan Chase Onsite Interview questions
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
- Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
- K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
- Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
- Two SumONSITE INTERVIEW · Seen Nov 2025
- Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
- Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
- Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024
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
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)
- JPMorgan Code for Good India: 10 Experiences (Part 4 of 5)