K Largest Elements with Quickselect
Reported by candidates from AMD's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The AMD OA reported in September 2026 looks like a heap problem, but the prompt rules the heap out. You're asked for the k largest values in nonincreasing order, duplicates kept, and the selection has to use in-place partitioning. That's quickselect, not a size-k priority queue. If you've only ever done this one with a min-heap, the constraint will throw you. The input can hit 200000 elements, so the average O(n) partition approach is the point. StealthCoder sits invisibly on your screen as a safety net if your partition logic goes blank mid-assessment.
The problem
Given an integer array nums and an integer k, return the k largest occurrences in nonincreasing order. Preserve duplicate occurrences. Your selection step should use in-place partitioning rather than maintaining a size-k heap. Sorting only the selected suffix for output order is allowed. Function kLargestQuickselect(nums: int[], k: int) → int[] Examples Example 1 nums = [3,2,1,5,6,4] k = 2 return = [6,5] Partitioning isolates 5 and 6, which are then ordered descending. Example 2 nums = [4,1,4,2,4] k = 3 return = [4,4,4] Duplicate occurrences are retained. Example 3 nums = [-5,-1,-3] k = 0 return = [] Selecting zero values returns an empty array. Constraints 0 <= nums.length <= 200000. -10^9 <= nums[i] <= 10^9. 0 <= k <= nums.length.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is quickselect on index n-k. Partition the array so the k largest land in the suffix, then sort only that suffix descending and return it. Duplicates are fine because you're selecting positions, not distinct values. The classic pitfall is a Lomuto partition on arrays with many equal values, like [4,1,4,2,4]. It degrades to O(n^2) and can time out at 200000 elements. Use a random pivot or a three-way partition to avoid it. Also handle k = 0 up front and return an empty array, and watch the empty-input case where nums.length is 0. Use pointer bounds carefully so you don't loop forever when the pivot lands at the target index. If you blank on the partition loop during the live OA, StealthCoder can supply a working version while you keep your own explanation steady.
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 K Largest Elements with Quickselect 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 AMD's OA.
AMD 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.
K Largest Elements with Quickselect FAQ
What's the trick in this AMD quickselect problem?+
Partition around a pivot until the pivot index equals n-k. Everything from that index onward is your k largest. Then sort just that suffix descending. You never build a heap, which is exactly what the prompt forbids.
Why not just use a heap or sort the whole array?+
The prompt says selection must use in-place partitioning rather than a size-k heap. A full sort would likely produce correct output but ignores the stated requirement, and it's O(n log n) instead of average O(n). Follow the constraint.
How do I handle duplicates and all-equal arrays?+
Duplicates are kept as separate occurrences, so [4,1,4,2,4] with k=3 gives [4,4,4]. For many equal values, use three-way partitioning or a random pivot so quickselect doesn't degrade to quadratic time.
What edge cases should I test first?+
Test k = 0 returning an empty array, k equal to nums.length, an empty nums array, negative numbers like [-5,-1,-3], and arrays full of identical values. These catch most off-by-one and infinite-loop bugs in the partition code.
How do I prepare for this in 48 hours?+
Write quickselect from scratch twice with a random pivot, then add the descending sort of the suffix. Run it on the three examples and an all-duplicates case. Know the average O(n) and worst-case O(n^2) story so you can explain it.