Hone logo
Hone
Problems

Find the Second Highest Score

This challenge asks you to process a collection of numerical scores and identify the second highest distinct score. This is a common pattern in data analysis, useful for ranking systems, identifying top performers just below the absolute best, or understanding data distribution beyond the maximum value.

Problem Description

You are given a collection (e.g., a list or array) of non-negative integer scores. Your task is to determine the second largest distinct score present in this collection.

Specifically, you need to:

  1. Identify all unique scores within the provided collection.
  2. From these unique scores, find the second largest one.
  3. If there is no second largest distinct score (e.g., the collection is empty, or it contains fewer than two distinct scores), you should indicate this by returning a special placeholder value.

Key Requirements:

  • The comparison should be based on distinct values. For example, if the scores are [10, 20, 20], the unique scores are [10, 20], and the second highest distinct score would be 10.
  • The solution must gracefully handle edge cases where a second highest distinct score might not exist.

Examples

Example 1:

Input: [50, 20, 70, 30, 90, 80]
Output: 80
Explanation: The distinct scores are 20, 30, 50, 70, 80, 90. The highest is 90, and the second highest is 80.

Example 2:

Input: [60, 60, 50, 40, 50]
Output: 50
Explanation: The distinct scores are 40, 50, 60. The highest is 60, and the second highest is 50. Duplicates are ignored when determining distinct scores.

Example 3:

Input: [100]
Output: NULL (or equivalent for 'not found')
Explanation: There is only one distinct score (100). There is no second highest distinct score.

Example 4:

Input: []
Output: NULL (or equivalent for 'not found')
Explanation: The collection is empty, so there are no scores, let alone a second highest.

Example 5:

Input: [70, 70, 70]
Output: NULL (or equivalent for 'not found')
Explanation: All scores are the same. There is only one distinct score (70), so no second highest.

Constraints

  • The input collection will contain between 0 and 100,000 integer scores.
  • Each score will be an integer between 0 and 1,000,000,000 (inclusive).
  • Your solution should aim for efficient processing, especially for large input collections.
  • If no second highest distinct score exists, the output should clearly represent this (e.g., NULL, None, -1 if specific context allows, or similar conceptually).

Notes

  • Remember to consider the definition of "distinct" carefully. Multiple occurrences of the same score should only count as one unique value.
  • Think about how your approach handles sorting and duplicate removal. Is it more efficient to sort first, then remove duplicates, or vice-versa?
  • Consider scenarios with very few elements (e.g., 0, 1, or 2) in your test cases. These are often where edge cases are missed.
  • While this problem might seem simple for small inputs, strive for a solution that performs well even with a very large number of scores.
Loading editor...
plaintext