Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    The k-th largest score

    MediumAbout 25 minFull AccessLearn it: Heaps with PriorityQueue in C#: Top K, Merge K Sorted, Running Median and API Pitfalls

    The task

    A game server shows the score that holds k-th place after each round, for example the score a player needs to reach the top ten.

    Complete KthLargest(int[] values, int k). Return the k-th largest value of values, where 1 ≤ k ≤ values.Length. Equal values count separately: in [5, 5, 3] the largest and the second largest are both 5. The array is never null.

    Signature

    public static int KthLargest(int[] values, int k)

    Examples

    • [3, 1, 4, 1, 5, 9, 2, 6], 3 → 5. From the top: 9, 6, then 5.
    • [5, 5, 5], 2 → 5. Equal values count separately.

    Limits: Up to 1,000,000 values; a value can be any int.

    The tests check: one value, k = 1 and k = the length, negative values, int.MinValue and int.MaxValue together, repeats, and 1,000,000 values with a small and a large k in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // Returns the k-th largest value; equal values count separately.
          public static int KthLargest(int[] values, int k)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.