Kth Largest Element in a Stream
Advanced
Kth Largest Element in a Stream
Welcome to the magical world of number streams! Imagine that numbers keep rolling in one after another, like a river constantly flowing. Your task is to figure out the k-th largest number in the current state of this stream.
Let's make this more fun! You're given a list of numbers and an integer k. Your mission, should you choose to accept it, is to find the k-th largest element in the list at any time.
Here's how it works:
- You'll receive a list of numbers and a number
k. - You'll find the
k-th largest number in the given list.
For example:
find_kth_largest([3, 1, 5, 12, 2, 14, 6], 3)
In this example, if you sort the numbers in descending order, you get [14, 12, 6, 5, 3, 2, 1]. The 3rd largest element here is 6. So, the function should return 6.
Can you make this happen? Let's see what you can do! Keep it fun and code on!
Examples:
find_kth_largest([4, 2, 9, 7, 5, 6, 7, 1, 3], 4) => 6 find_kth_largest([15, 20, 30, 5], 2) => 20 find_kth_largest([3], 1) => 3 find_kth_largest([11, 15, 3, 7, 10], 5) => 3
Inputs
Arg 1:
[3,1,5,12,2,14,6]Arg 2:
3Expected Output
6Actual Output