The Bronze Medalist

An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
easy
Seniority
L3

Problem

Given a list of integers and integer k, return the k-th largest distinct-position value (by rank; k=1 is the max). You may use built-in sorting.

Not first, not last - somewhere in the middle of the podium.

Example 1

Input:

nums = [3,2,1,5,6,4], k = 2

Output:

5

Example 2

Input:

nums = [3,2,3,1,2,4,5,5,6], k = 4

Output:

4

Worked solution and explanation

Why this problem exists in real interviews

Finding the k-th largest element tests sorting and indexing. While the prompt allows built-in sorting, interviewers use this to transition into discussions about quickselect and heap-based approaches.


Break down the requirements

Step 1: Sort the list in descending order

This places the largest element at index 0, second largest at index 1, etc.

Step 2: Return the element at index k-1

The k-th largest is at position k-1 in a 0-indexed descending-sorted list.


The solution

Sort descending and index for k-th largest
def kth_largest(nums, k):
    sorted_nums = sorted(nums, reverse=True)
    result = sorted_nums[k - 1]
    return result

Common follow-up questions