Reported September 2026
Salesforceheap priority queue

Kth Largest Element in an Array

Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Salesforce OA. Under 2s to a working solution.
Founder's read

Salesforce reported this one in September 2026, and it's Kth Largest Element in an Array. Strip the wording and it's a selection problem: you don't need the whole array sorted, you need one rank. Duplicates count separately, so [5,5] fills two slots. If you've got an OA invite and 48 hours, know three routes: sort, min-heap of size k, and quickselect. The value range is tiny (-10^4 to 10^4), which opens a counting shortcut too. StealthCoder sits invisibly on your screen as a safety net if you freeze mid-assessment, but you should walk in already knowing which route you'll pick.

The problem

Given an integer array nums and an integer k, return the kth largest element in sorted order.
For this exercise, assume duplicate occurrences count separately; the answer is not the kth distinct value.

Function
findKthLargest(nums: int[], k: int) → int

Examples
Example 1
nums = [3,2,1,5,6,4]
k = 2
return = 5
Descending order is [6,5,4,3,2,1], whose second value is 5.
Example 2
nums = [3,2,3,1,2,4,5,5,6]
k = 4
return = 4
The two occurrences of 5 occupy separate ranks, so the fourth largest occurrence is 4.

Constraints
1 <= k <= nums.length <= 10^5.
-10^4 <= nums[i] <= 10^4.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The real reduction: find the element that would land at index n-k in ascending order. Sorting works in O(n log n) and passes at n up to 10^5. The cleaner answer is a min-heap capped at size k. Push each number, pop when size exceeds k, and the root is your answer in O(n log k). Quickselect gets O(n) average by partitioning around a pivot and only recursing into the side that holds index n-k. The pitfall is duplicates. Don't dedupe with a set, because the problem says each occurrence ranks separately. The other trap is quickselect on many equal values, which degrades without a random pivot or three-way partition. Because values sit in a narrow range, a counting array of size 20001 also works in linear time. If you blank on the live OA, StealthCoder is the hedge that hands you the heap version fast, so the choice of approach never costs you the clock.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Kth Largest Element in an Array 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as kth largest element in an array. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Salesforce's OA.

Salesforce 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 Largest Element in an Array FAQ

How hard is Kth Largest Element in an Array really?+

It's a medium on paper, but the sort-then-index answer passes at n up to 10^5. The difficulty is in explaining a better approach if asked. Know the heap solution cold, since it's short and hard to get wrong under pressure.

What's the trick to solving it fast?+

Keep a min-heap of size k. Push each number, pop the smallest when the heap grows past k. After one pass the root is the kth largest. It's about six lines in most languages and runs in O(n log k).

Do duplicates count as one value?+

No. The problem states duplicate occurrences count separately. In [3,2,3,1,2,4,5,5,6] with k=4, the two 5s take ranks 1 and 2 and 4 is the answer. Don't use a set or you'll get the wrong output.

Should I use quickselect or a heap?+

Heap is safer for an OA. Quickselect is O(n) average but needs a random pivot or three-way partition to avoid worst-case slowdowns on repeated values. Use quickselect only if you've written it recently and can do it without bugs.

How do I prepare in 48 hours for this Salesforce question?+

Write three versions from memory: sort and index, min-heap of size k, and counting array using the -10^4 to 10^4 range. Test on both examples and on k=1 and k=n edge cases. That covers every variation they could ask.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Salesforce.

OA at Salesforce?
Invisible during screen share
Get it