Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
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".
Example 2:
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".
Example 3:
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 <= 1041 <= nums[i].length <= 100nums[i] consists of only digits.nums[i] will not have any leading zeros.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["3","6","7","10"] 4
["2","21","12","1"] 3
["0","0"] 2
kth-largest-element-in-an-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1985: Find the Kth Largest Integer in the Array
class Solution {
public String kthLargestNumber(String[] nums, int k) {
Arrays.sort(
nums, (a, b) -> a.length() == b.length() ? b.compareTo(a) : b.length() - a.length());
return nums[k - 1];
}
}
// Accepted solution for LeetCode #1985: Find the Kth Largest Integer in the Array
func kthLargestNumber(nums []string, k int) string {
sort.Slice(nums, func(i, j int) bool {
a, b := nums[i], nums[j]
if len(a) == len(b) {
return a > b
}
return len(a) > len(b)
})
return nums[k-1]
}
# Accepted solution for LeetCode #1985: Find the Kth Largest Integer in the Array
class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
return nlargest(k, nums, key=lambda x: int(x))[k - 1]
// Accepted solution for LeetCode #1985: Find the Kth Largest Integer in the Array
/**
* [1985] Find the Kth Largest Integer in the Array
*
* 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 k^th 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.
*
* Example 1:
*
* 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 4^th largest integer in nums is "3".
*
* Example 2:
]*
* 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 3^rd largest integer in nums is "2".
*
* Example 3:
*
* Input: nums = ["0","0"], k = 2
* Output: "0"
* Explanation:
* The numbers in nums sorted in non-decreasing order are ["0","0"].
* The 2^nd 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.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/
// discuss: https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn kth_largest_number(nums: Vec<String>, k: i32) -> String {
let mut nums = nums;
let k = (k - 1) as usize;
nums.select_nth_unstable_by(k, |a, b| (b.len()).cmp(&a.len()).then_with(|| b.cmp(&a)));
nums[k].clone()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1985_example_1() {
let nums = vec_string!["3", "6", "7", "10"];
let k = 4;
let result = "3".to_string();
assert_eq!(Solution::kth_largest_number(nums, k), result);
}
#[test]
fn test_1985_example_2() {
let nums = vec_string!["2", "21", "12", "1"];
let k = 3;
let result = "2".to_string();
assert_eq!(Solution::kth_largest_number(nums, k), result);
}
#[test]
fn test_1985_example_3() {
let nums = vec_string!["0", "0"];
let k = 2;
let result = "0".to_string();
assert_eq!(Solution::kth_largest_number(nums, k), result);
}
}
// Accepted solution for LeetCode #1985: Find the Kth Largest Integer in the Array
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1985: Find the Kth Largest Integer in the Array
// class Solution {
// public String kthLargestNumber(String[] nums, int k) {
// Arrays.sort(
// nums, (a, b) -> a.length() == b.length() ? b.compareTo(a) : b.length() - a.length());
// return nums[k - 1];
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.