Kth largest order

sorting

Kth largest order

Instacart Python Interview Question

Instacart's rewards team gives a bonus to the shopper who handled the kth biggest order of the day.

Write a function kth_largest(order_totals, k) that returns the kth largest value in the list, where k = 1 means the largest. Repeated values count separately, so in [5, 5, 3] the second largest is 5. k is always between 1 and the length of the list.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

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

Output

5

Example 2

Input

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

Output

4

Explanation

In the first example, the totals from largest to smallest are 6, 5, 4, 3, 2 and 1, so the second largest is 5. In the second example, the totals sorted from largest are 6, 5, 5, 4, 3, 3, 2, 2 and 1, so the fourth largest is 4.

Submit also runs 4 hidden test cases that check edge cases.