Determine Whether a String Is a Valid Number
eBaySoftware Engineer · Technical ScreenLast updated
Implement a strict numeric string validator that checks whether an ASCII string conforms to valid integer, decimal, and scientific notation formats.
Problem Statement
Given an ASCII string s, determine if it represents a valid number according to the following strict grammar rules:
- No Whitespace: Leading, trailing, or embedded whitespace characters are not allowed.
- Number Form: A valid number must be either a valid integer or a valid decimal, optionally followed by an exponent component.
- Integer: An optional sign (
'+'or'-'), followed by one or more digits (0-9). - Decimal: An optional sign (
'+'or'-'), followed by one of the following patterns:- One or more digits followed by a dot
.(e.g.,"2.") - One or more digits followed by a dot
.followed by one or more digits (e.g.,"0.45") - A dot
.followed by one or more digits (e.g.,".8") (Note: A standalone dot"."or"+."is invalid; at least one digit must be present).
- One or more digits followed by a dot
- Exponent: An
'e'or'E'character followed by a valid integer (an optional sign'+'or'-'followed by one or more digits). Exponents cannot contain decimal points or additional exponents. - Unsupported Forms: Hexadecimal (
0x...), binary (0b...), octal, commas, and other notations are strictly invalid.
Return True if s is a valid number, and False otherwise.
Function Signature
def is_valid_number(s: str) -> bool:
pass
Constraints
1 <= len(s) <= 100sconsists of English letters, digits (0-9),'+','-','.', and whitespace characters.
Examples
Example 1
Input: s = "-0.1e+5"
Output: True
Explanation: The base is a valid decimal "-0.1", followed by an exponent 'e' with a signed integer "+5".
Example 2
Input: s = "1e2.5"
Output: False
Explanation: The exponent part must be an integer, but "2.5" contains a decimal point.
Example 3
Input: s = " .1 "
Output: False
Explanation: Whitespace is not permitted.
Edge Cases to Consider
- Dot without digits before or after:
".","+."(Invalid) - Missing digits around exponent:
"e","1e","e3"(Invalid) - Multiple signs or misplaced signs:
"++1","1+"(Invalid) - Multiple dots or dots in exponent:
"1.2.3","1e2.0"(Invalid)
Hints
Hint 1
Track boolean flags as you iterate through the string: `seen_digit`, `seen_dot`, and `seen_exponent`.
Hint 2
A sign character (`'+'` or `'-'`) can only appear at index 0 or immediately after an `'e'` or `'E'`.
Hint 3
A dot (`'.'`) cannot appear after an exponent has already been seen, and cannot appear more than once.
Follow-up questions
- How would you model this validator using a Deterministic Finite Automaton (DFA) state transition table?
- If this validator needed to actually parse the string into a 64-bit IEEE 754 float without using built-in parsing libraries, how would you handle mantissa and exponent overflow?
- How would you extend this logic to support alternate representations like hexadecimal floating-point literals (e.g., `0x1.fp3`)?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.