> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find the Kth Largest Integer in the Array

> Tested Python solution for LeetCode 1985 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1985, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Divide and Conquer](/catalog/topics/divide-and-conquer), [Sorting](/catalog/topics/sorting), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue), Quickselect. [View on LeetCode](https://leetcode.com/problems/kth-largest-number-in-array/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1985   # by problem number
lcpy gen -s kth_largest_number_in_array   # by problem name
```

## Problem

You are given an array of strings `nums` and an integer `k`. Each string in `nums` represents an integer without leading zeros.

Return the string that represents the `kth` largest integer in `nums`.

**Note**: Duplicate numbers should be counted distinctly. For example, if `nums` is `["1","2","2"]`, `"2"` is the first largest integer, `"2"` is the second-largest integer, and `"1"` is the third-largest integer.

### Examples

```
Input: nums = ["3","6","7","10"], k = 4
Output: "3"
Explanation: The numbers in nums sorted in non-decreasing order are ["3","6","7","10"]. The 4th largest integer in nums is "3".
```

```
Input: nums = ["2","21","12","1"], k = 3
Output: "2"
Explanation: The numbers in nums sorted in non-decreasing order are ["1","2","12","21"]. The 3rd largest integer in nums is "2".
```

```
Input: nums = ["0","0"], k = 2
Output: "0"
Explanation: The numbers in nums sorted in non-decreasing order are ["0","0"]. The 2nd largest integer in nums is "0".
```

### Constraints

* 1 \<= k \<= nums.length \<= 10^4
* 1 \<= nums\[i].length \<= 100
* nums\[i] consists of only digits.
* nums\[i] will not have any leading zeros.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/kth_largest_number_in_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/kth_largest_number_in_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import heapq


class Solution:
    # Time: O(n log k)
    # Space: O(k)
    def kth_largest_number(self, nums: list[str], k: int) -> str:
        heap: list[tuple[int, str]] = []
        for num in nums:
            key = (len(num), num)
            if len(heap) < k:
                heapq.heappush(heap, key)
            elif key > heap[0]:
                heapq.heapreplace(heap, key)
        return heap[0][1]
```

## Complexity

| Time | Space |
| - | - |
| O(n log k) | O(k) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.