Return the k highest server loads

Write codeEasy10 min

Your task

Each server in a fleet reports its current CPU load as an integer. A dashboard shows the busiest machines first.

Write top_server_loads(loads, k) that returns the k highest loads, highest first.

Input and output:

  • loads: a list of up to 100,000 integers, each between 0 and 1,000,000. It may be empty.
  • k: an integer, 0 or more.
  • Returns a list of integers in descending order.

Rules:

  • Each report counts on its own, so equal loads can appear more than once in the result.
  • If k is larger than the number of loads, return every load.
  • If k is 0 or loads is empty, return an empty list.

Examples

top_server_loads(loads: list[int], k: int) -> list[int]
Example 1
Inputtop_server_loads([62, 95, 40, 73], 2)
Output[95, 73]
Example 2
Inputtop_server_loads([3, 9, 9, 1], 3)
Output[9, 9, 3]
Example 3
Inputtop_server_loads([4, 1], 5)
Output[4, 1]