Largest Odd Slice of a Decimal String

Quick Overview

Largest Odd Slice of a Decimal String. The digit string num describes a decimal integer without leading zeros. Return the substring with the greatest numerical value among all nonempty odd-valued substrings. Return an empty string when none exists.

Largest Odd Slice of a Decimal String

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given a decimal digit string `num` with no leading zeros, return the numerically largest odd integer represented by one nonempty contiguous substring. Return that substring as a string, or return `""` if there is no odd-valued substring. ```cpp class Solution { public: string largestOddNumber(string num); }; ``` ### Examples **Example 1:** ```text num = "82413" Output: "82413" Explanation: The entire number is odd and dominates every shorter substring. ``` **Example 2:** ```text num = "70624" Output: "7" Explanation: Only the initial digit is odd, so every odd substring ends there. ``` ### Constraints - `1 <= num.length <= 10^5`. - All characters are decimal digits; there are no leading zeros. ### Clarifying Questions Substrings must be contiguous. Return decimal text rather than converting the potentially very large number to a machine integer. ```hint Inspect the structure Which final digit makes a decimal integer odd, and how far left can that substring extend? ``` ### Reasoning checkpoints - Justify why the returned substring has the greatest numerical value among every valid candidate. - Handle strings with no odd-valued substring and avoid overflow for the largest permitted length. ### Optional extensions How would permitting leading zeros affect the comparison argument?

Overview: Largest Odd Slice of a Decimal String. The digit string num describes a decimal integer without leading zeros. Return the substring with the greatest numerical value among all nonempty odd-valued substrings. Return an empty string when none exists.

Amazon logo
Amazon
May 29, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given a decimal digit string num with no leading zeros, return the numerically largest odd integer represented by one nonempty contiguous substring. Return that substring as a string, or return "" if there is no odd-valued substring.

class Solution {
public:
    string largestOddNumber(string num);
};

Examples

Example 1:

num = "82413"
Output: "82413"
Explanation: The entire number is odd and dominates every shorter substring.

Example 2:

num = "70624"
Output: "7"
Explanation: Only the initial digit is odd, so every odd substring ends there.

Constraints

  • 1 <= num.length <= 10^5 .
  • All characters are decimal digits; there are no leading zeros.

Clarifying Questions Guidance

Substrings must be contiguous. Return decimal text rather than converting the potentially very large number to a machine integer.

Reasoning checkpoints

  • Justify why the returned substring has the greatest numerical value among every valid candidate.
  • Handle strings with no odd-valued substring and avoid overflow for the largest permitted length.

Optional extensions

How would permitting leading zeros affect the comparison argument?