This is a standard "kth largest element" problem with one complication: the numbers are represented as strings, and they can have up to 100 digits. That is far beyond what a long or even a 64-bit integer can hold, which maxes out at about 19 digits.
So we cannot convert these strings to integers and compare them directly. We need a comparison that works on the string representation itself. Because there are no leading zeros, comparing two numeric strings is direct: a longer string always represents a larger number, and strings of equal length can be compared lexicographically, character by character.
Unlike some "kth largest" problems, duplicates here count as separate entries. For ["2","2","1"] with k=2, the answer is "2" (the second copy), not "1".
1 <= k <= nums.length <= 10^4 → With n up to 10,000, an O(n log n) sort is well within limits, and an O(n log k) heap or O(n) average quickselect are also options.1 <= nums[i].length <= 100 → Numbers can have up to 100 digits, which rules out converting to any integer type. Every comparison is a string comparison costing O(m) where m is the string length.Sort the entire array of numeric strings, then pick the kth element from the end. This needs a custom comparator. A plain lexicographic sort ranks "9" above "10" (because '9' > '1'), which is numerically wrong. Comparing lengths first fixes that: with no leading zeros, a longer string is always a larger number, and for equal-length strings lexicographic order matches numeric order.
n - k.Sorting orders the entire array even though we only need one position. The next approach tracks the top k elements during a single scan and discards the rest.
Instead of sorting everything, use a min-heap (priority queue) that holds at most k elements. Add each string to the heap. When the heap grows beyond size k, remove the smallest element. After processing all elements, the heap holds exactly the k largest values, and the smallest of those (the heap top) is the kth largest overall.
A min-heap fits here because the element we need fast access to is the smallest of the current top-k. That element is both the eviction candidate when a larger value arrives and the final answer.
The invariant is that after processing any prefix of the array, the heap contains the k largest values seen so far (or all of them if fewer than k have been seen). When a new value arrives and the heap is full, it is only discarded if it is smaller than the current minimum, so it could not belong to the top-k. Any value larger than the minimum displaces it, which can only improve the set. After the full pass, the heap holds the k largest values overall, and its minimum is the kth largest.
The heap maintains an ordered structure of k elements throughout the scan, though we only read one value at the end. The next approach partitions the array around a pivot to locate the kth largest directly, without keeping any element ordered.
Quickselect is the selection-algorithm analog of quicksort. Instead of sorting the entire array, it recurses only into the partition that contains the target index. On average each step reduces the search range by a constant factor, giving O(n) expected time.
The mechanism: pick a pivot and partition the array so smaller elements end up on one side and larger ones on the other, with the pivot in its final sorted position. If the target index falls in the left section, recurse left; if it falls in the right section, recurse right; if it lands among the elements equal to the pivot, return the pivot.
Worst-case performance is O(n^2), which occurs when the chosen pivots repeatedly split off only one element. Choosing the pivot at random makes a long run of such splits improbable, so the expected running time stays linear.
n - k (the kth largest in ascending order).