FastPrepKth Greater Element Indexes
Problems/Kth Greater Element Indexes

Kth Greater Element Indexes

Uber logoUberFULLTIMEOA

Updated Jul 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given an integer array nums and an integer k. For each index i, look at the elements to the right of i whose value is strictly greater than nums[i], preserving their original left-to-right order.

Return an integer array answer where answer[i] is the 1-indexed position of the k-th such greater element for index i. If fewer than k greater elements appear to the right of i, set answer[i] to -1.

Function

kthGreaterElementIndexes(nums: int[], k: int) → int[]

Examples

Example 1

nums = [3, 4, 2, 6, 5]k = 2return = [4, 5, 5, -1, -1]

For 3, the greater elements to the right are 4, 6, 5, so the 2nd one is 6 at 1-indexed position 4. For 4, the greater elements are 6, 5, so the answer is position 5.

Example 2

nums = [5, 1, 4, 2, 3]k = 1return = [-1, 3, -1, 5, -1]

For 1, the first greater element to its right is 4 at position 3. For 2, it is 3 at position 5.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= nums.length
  • -10^9 <= nums[i] <= 10^9

More Uber Online Assessment questions

  • Count Palindromic Ancestor PathsOA · Seen Aug 2026
  • Jump Game with Prime-3 StepsOA · Seen Aug 2026
  • Last Truck to Leave the LaneOA · Seen Jul 2026
  • Chain of CommandOA · Seen Jul 2026
  • Total Palindrome Substring CostOA · Seen Jun 2026
  • Jump Game with Prime-Step RuleOA · Seen May 2026
  • Pipeline Throughput Maximization Under BudgetOA · Seen May 2026
  • Balanced Prefix Sets in a PermutationOA · Seen Apr 2026
  • Minimum Edge Reversals to Root a TreeOA · Seen Apr 2026
  • Maximum Riders in CarOA · Seen Apr 2026
All Uber interview questionsSee Uber hiring insights
Saved
class Solution {
    public int[] kthGreaterElementIndexes(int[] nums, int k) {
      // write your code here
    }
}
Checking account…