Given a list of integers and an integer k, return the kth largest element in the list. The kth largest is the kth element in sorted descending order, where 1st largest equals the maximum value. If k is less than 1 or greater than the list size, return null. If the list is null or empty, return null. Duplicate values count as separate positions.

Examples:

Input: numbers = [3, 2, 1, 5, 6, 4], k = 2 Output: 5 Explanation: Sorted descending: [6, 5, 4, 3, 2, 1], 2nd largest = 5 Input: numbers = [3, 2, 3, 1, 2, 4, 5, 5, 6], k = 4 Output: 4 Explanation: Sorted descending: [6, 5, 5, 4, 3, 3, 2, 2, 1], 4th largest = 4 Input: numbers = [1], k = 1 Output: 1 Explanation: Single element is the 1st largest Input: numbers = [1, 2, 3], k = 0 Output: null Explanation: k must be at least 1 Input: numbers = [1, 2, 3], k = 5 Output: null Explanation: k exceeds list size Input: numbers = null, k = 1 Output: null Explanation: Null list returns null Input: numbers = [], k = 1 Output: null Explanation: Empty list returns null
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay Mar 2026

Contest runs March 3 - 31. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.