Kth Greater Element Indexes
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Uber OA, reported in February 2026, is writing the obvious nested loop and watching it die at n = 10^5. The problem looks like a next-greater-element variant, but you need the k-th greater value to the right, not the first. Candidates who recognize it as an offline ordering problem with a binary-search flavor move fast. Candidates who don't burn twenty minutes on a stack that can't answer k > 1. If you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the approach live.
The problem
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 = 2 return = [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 = 1 return = [-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
Reported by candidates. Source: FastPrep
Pattern and pitfall
Brute force is O(n^2) and times out at 10^5. The trick is to process values in descending order and keep a sorted structure of positions. Sort indices by value, and for each group of equal values, query before inserting them, since strictly greater means equal values must not count. For index i, you need the k-th position greater than i among inserted positions. Use a Fenwick tree over positions with count prefix sums, then binary-search descent to find the (rank of i + k)-th inserted position. Rank is the count of inserted positions up to i. If total inserted is below that, answer is -1. The common pitfall is inserting equal values before querying, which wrongly counts ties. Another is forgetting the answer is 1-indexed. If the Fenwick descent feels shaky under pressure, StealthCoder is the hedge for the live OA, since it reads the problem and hands you a working solution.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Kth Greater Element Indexes cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Kth Greater Element Indexes FAQ
What's the actual trick for Kth Greater Element Indexes?+
Process values from largest to smallest and track the positions of larger elements in a Fenwick tree or order-statistic structure. For each index, find the k-th inserted position after it. Handle ties by querying a whole equal-value group before inserting any of them.
How hard is this really?+
Medium-hard. The idea is simple once you see it, but the data structure is the hurdle. A Fenwick tree with a binary-descent k-th search is the clean path. If you only know monotonic stacks, you'll get stuck because they only handle the first greater element.
Why does a monotonic stack fail here?+
A monotonic stack finds the nearest greater element, which is the k = 1 case. For k-th, you need to count greater elements to the right and locate the k-th one. That requires ordered counting, not a single pass of stack pops.
What edge cases should I test?+
Test duplicates, since strictly greater excludes equal values. Test k larger than the number of greater elements, which gives -1. Test a descending array where every answer is -1, and a single-element array. Also confirm you return 1-indexed positions, not 0-indexed.
How do I prepare for this in 48 hours?+
Write a Fenwick tree with prefix sum and k-th element search from scratch until it's automatic. Then solve this problem offline by sorting values descending. Check your tie handling on Example 1 and 2 by hand. Aim for O(n log n) and confirm it fits n = 10^5.