Find the First Target Index
Updated
Problem statement
Given a nondecreasing array of integers nums and an integer target, return the smallest index i such that nums[i] == target.
If target does not appear in nums, return -1.
Function
firstIndex(nums: int[], target: int) → intExamples
Example 1
nums = [1,2,2,2,5,8]target = 2return = 1The target appears at indices 1, 2, and 3, so the first index is 1.
Example 2
nums = [-5,-1,0,4,4,9]target = 4return = 3The first value equal to 4 is at index 3.
Example 3
nums = [1,3,5,7]target = 4return = -1No array value equals 4, so the result is -1.
Constraints
0 <= nums.length <= 10^5.-2^31 <= nums[i], target <= 2^31 - 1.numsis sorted in nondecreasing order.- Your algorithm should run in
O(log n)time.
More Superhuman interview questions
- Email Funnel: Opened Within a Day, Replied Within a WeekPHONE SCREEN · Seen Nov 2026
- Intersect Two Interval ListsPHONE SCREEN · Seen Jul 2026
- Deep Copy a Random-Pointer ListPHONE SCREEN
- Deep Copy a Singly Linked ListPHONE SCREEN
- Recover a Tree from Preorder Depth EncodingPHONE SCREEN
- Reverse a Singly Linked ListPHONE SCREEN
- Longest Distinct StringOA · Seen Sep 2026
- Minimum Time on Two ProcessorsOA · Seen Sep 2026
- Top Mutual-Friend RecommendationsOA · Seen Sep 2026
- Lost MessagesOA · Seen Apr 2026
Similar questions at other companies
- 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
- First Bad Version in a Monotone ArrayMeta · PHONE SCREENMeta 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
- First Target IndexGrammarly · PHONE SCREEN
- Ceiling Value in a Sorted ArrayPeopleStrong · PHONE SCREENPeopleStrong interview questions