First Element Greater Than K
Updated
Problem statement
Given an integer array nums sorted in non-decreasing order and an integer k, return the first element of nums that is strictly greater than k. Because nums is sorted, this is also the smallest element greater than k.
Elements equal to k do not count. If no element is greater than k, including when nums is empty, return -1.
Follow-up: can you do it in O(log n) time, where n is the length of nums?
Function
firstGreater(nums: int[], k: int) → intExamples
Example 1
nums = [1,3,3,5,8]k = 3return = 5The two copies of 3 are equal to k, not greater, so they are skipped. The first element greater than 3 is 5.
Example 2
nums = [2,4,6]k = 6return = -1The largest element, 6, equals k. No element is greater than 6, so return -1.
Example 3
nums = [2,5,9,14]k = 10return = 14k = 10 is not in the array. The elements 2, 5 and 9 are smaller, and 14 is the first one greater than 10.
Constraints
0 <= nums.length <= 10^40 <= nums[i] <= 10^90 <= k <= 10^9numsis sorted in non-decreasing order (values may repeat).
More Meta Phone Screen questions
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Values Exclusive to the Current UserPHONE SCREEN · Seen Sep 2026
- First Bad Version in a Monotone ArrayPHONE SCREEN · Seen Sep 2026
- Parenthesized String CalculatorPHONE SCREEN · Seen Sep 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
- 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
- First Target IndexGrammarly · PHONE SCREEN
- Ceiling Value in a Sorted ArrayPeopleStrong · PHONE SCREENPeopleStrong interview questions