KatInterview

Strict Numeric String Recognizer

StringsAuto-graded · 31 test cases
MediumAuto-gradedStringsstringsparsingtwo-pointers

Last updated

Tests whether candidates can implement a precise single-pass parser that accepts only well-formed integers, decimals, and scientific notation strings.

Problem Statement

Given a string s made up of ASCII characters, write a function that decides whether s encodes a valid number under the following strict rules.

Allowed Structure

A valid number is a base optionally followed by an exponent. Nothing else is permitted — no surrounding spaces, no extra characters.

Base — exactly one of:

  • A whole number: an optional + or - sign, then one or more decimal digits (0–9).

  • A decimal number: an optional + or - sign, then one of:

    • one or more digits, a dot ., then one or more digits ("0.45")
    • one or more digits followed by a dot with nothing after it ("2.")
    • a dot followed by one or more digits with nothing before it (".8")

    A lone dot, or a signed lone dot such as "+.", is not a valid base — at least one digit must appear on some side of the dot.

Exponent (optional) — the letter e or E, then a valid whole number (optional sign, one or more digits). The exponent portion may not itself contain a dot or a nested exponent.

What is not allowed

  • Whitespace anywhere in the string (leading, trailing, or embedded).
  • A second dot, a dot inside the exponent, or two exponent markers.
  • A sign that appears anywhere other than immediately after the start of the string or immediately after e/E.
  • Alternative number notations: hexadecimal (0x…), binary (0b…), octal, underscore separators, commas, inf, nan, etc.

Return True if s is a valid number under these rules, and False otherwise.

Constraints

  • 1 <= len(s) <= 100
  • s consists of ASCII letters, decimal digits (0–9), '+', '-', '.', and whitespace characters.

Examples

Example 1

Input: is_valid_number('-0.1e+5')

Output: True

Example 2

Input: is_valid_number('1e2.5')

Output: False

Example 3

Input: is_valid_number(' .1 ')

Output: False

Example 4

Input: is_valid_number('2.')

Output: True

Hints

Hint 1

Walk the string left-to-right with a single index. Consume each grammar piece in order — base sign, integer digits, optional dot and fractional digits, optional exponent — and check at the end that the index reached exactly len(s).

Hint 2

A correct base needs at least one digit total, counting both the digits before the dot and the digits after it. Use two separate counters so you can enforce this rule regardless of whether a dot appeared.

Hint 3

For the exponent branch: only enter it when you actually see an 'e' or 'E', advance past it, allow one optional sign, then require at least one digit. If you enter the branch but find zero digits, the whole string is invalid. Any character the grammar never consumed — a space, a second dot, a letter like 'x' — will leave the index short of the end and fail the final check.