Find the Second-Greatest Distinct Digit

Quick Overview

Find the Second-Greatest Distinct Digit. The string s contains lowercase letters and decimal digits. Return the second-greatest distinct digit value occurring in it, or -1 when fewer than two different digits occur.

Find the Second-Greatest Distinct Digit

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem The string `s` contains lowercase letters and decimal digits. Return the second-greatest distinct digit value occurring in it, or `-1` when fewer than two different digits occur. ```cpp class Solution { public: int secondHighest(string s); }; ``` ### Examples **Example 1:** ```text s = "x90889y" Output: 8 Explanation: Distinct digits are 0, 8, and 9. ``` **Example 2:** ```text s = "a000b" Output: -1 Explanation: Repeated zero is still only one distinct digit. ``` ### Constraints - `1 <= s.length <= 500`. - Every character is a lowercase English letter or a digit. ### Clarifying Questions Repeated copies of the largest digit do not create a second-largest distinct value. Return an integer digit, not its character code. ```hint Inspect the structure How do repeated digits, letters, and zero affect the meaning of second-greatest distinct digit? ``` ### Reasoning checkpoints - Exclude letters and repeated occurrences when ranking distinct digit values. - Handle zero as a valid digit and distinguish it from the missing-result sentinel. ### Optional extensions How could two running maxima replace the presence table?

Overview: Find the Second-Greatest Distinct Digit. The string s contains lowercase letters and decimal digits. Return the second-greatest distinct digit value occurring in it, or -1 when fewer than two different digits occur.

Meta logo
Meta
Jun 1, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

The string s contains lowercase letters and decimal digits. Return the second-greatest distinct digit value occurring in it, or -1 when fewer than two different digits occur.

class Solution {
public:
    int secondHighest(string s);
};

Examples

Example 1:

s = "x90889y"
Output: 8
Explanation: Distinct digits are 0, 8, and 9.

Example 2:

s = "a000b"
Output: -1
Explanation: Repeated zero is still only one distinct digit.

Constraints

  • 1 <= s.length <= 500 .
  • Every character is a lowercase English letter or a digit.

Clarifying Questions Guidance

Repeated copies of the largest digit do not create a second-largest distinct value. Return an integer digit, not its character code.

Reasoning checkpoints

  • Exclude letters and repeated occurrences when ranking distinct digit values.
  • Handle zero as a valid digit and distinguish it from the missing-result sentinel.

Optional extensions

How could two running maxima replace the presence table?