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
def kth_largest(nums, k):
sorted_nums = sorted(nums, reverse=True)
result = sorted_nums[k - 1]
return resultCommon follow-up questions
- Can you solve this in O(n) average time? (Tests quickselect: partition around a pivot and recurse into the relevant half.)
- What if k is very small relative to n? (Tests using a min-heap of size k for O(n log k) time.)
- What if the list contains duplicates? (Tests clarifying whether 'k-th largest' means distinct values or by position.)