First Target Index
Updated
Problem statement
Given a nondecreasing integer array nums and an integer target, return the smallest index i such that nums[i] == target. Return -1 when target does not occur.
Your solution must run in O(log n) time.
Function
firstTargetIndex(nums: int[], target: int) → intExamples
Example 1
nums = [1,2,2,2,4]target = 2return = 1The target appears at indices 1, 2, and 3, so the first index is 1.
Example 2
nums = [-5,-2,0,3]target = 1return = -1The target does not occur in nums.
Example 3
nums = [7]target = 7return = 0The only element equals the target.
Constraints
1 <= nums.length <= 100000.-2147483648 <= nums[i], target <= 2147483647.numsis sorted in nondecreasing order.
More Grammarly interview questions
Similar questions at other companies
- First Element Greater Than KMeta · PHONE SCREENMeta interview questions
- Track New Values in Sorted OrderBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Price at Query TimestampsGoogle · PHONE SCREENGoogle interview questions
- First Missing Number in a Consecutive ArrayArista Networks · OA · PHONE SCREENArista Networks interview questions
- Messages in an Inclusive Timestamp RangeAttentive · PHONE SCREEN · ONSITE INTERVIEWAttentive interview questions
- Target Sum Across Two Sorted ArraysSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Leftmost Binary Search MatchByteDance · PHONE SCREENByteDance interview questions
- Count Occurrences in a Sorted ArrayAdobe · PHONE SCREEN · ONSITE INTERVIEWAdobe interview questions
- Find the First Target IndexSuperhuman · PHONE SCREENSuperhuman interview questions
- Ceiling Value in a Sorted ArrayPeopleStrong · PHONE SCREENPeopleStrong interview questions