Complexity Analysis
def kth_largest(arr, k):
arr.sort(reverse=True)
return arr[k - 1]
What is the time and space complexity?
Work it out before scrolling down.
Time
O(n log n)
Space
O(1)
How to derive it
Sorting dominates at O(n log n). Indexing is O(1). A heap-based approach could do O(n log k) or quickselect could do O(n) average. When you see a sort followed by a constant-time lookup, the sort is the bottleneck.