Validate a simplified numeric string
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates string-parsing, formal-grammar validation, and edge-case reasoning competencies, including the ability to recognize valid numeric formats and to provide time and space complexity analysis.
Constraints
- 0 <= len(s) <= 10^5
- s consists of printable ASCII characters; any character outside the allowed set (sign, digit, '.', 'e'/'E') makes s invalid.
- An empty string is invalid.
- At most one decimal point and at most one exponent are permitted.
Examples
Input: ('123',)
Expected Output: True
Explanation: Plain integer with no sign, dot, or exponent — valid.
Input: ('0.5',)
Expected Output: True
Explanation: Single decimal point between digits — valid mantissa.
Input: ('.8',)
Expected Output: True
Explanation: Decimal point may lead the mantissa as long as a digit follows.
Input: ('10.',)
Expected Output: True
Explanation: A trailing decimal point is allowed because at least one digit precedes it.
Input: ('1e9',)
Expected Output: True
Explanation: Exponent 'e' followed directly by a digit — valid.
Input: ('-3.2E-4',)
Expected Output: True
Explanation: Leading sign, decimal mantissa, and a signed exponent — all valid together.
Input: ('+6',)
Expected Output: True
Explanation: Optional leading '+' sign followed by a digit — valid.
Input: ('e3',)
Expected Output: False
Explanation: Exponent with no mantissa digit before it — invalid.
Input: ('+.',)
Expected Output: False
Explanation: Sign and a lone dot with no digit — mantissa has no digit, invalid.
Input: ('1.2.3',)
Expected Output: False
Explanation: Two decimal points are not allowed.
Input: ('--6',)
Expected Output: False
Explanation: Multiple leading signs are not allowed.
Input: ('6e',)
Expected Output: False
Explanation: Exponent marker with no digit following it — invalid.
Input: ('6e-',)
Expected Output: False
Explanation: Exponent has a sign but no digit after it — invalid.
Input: ('.',)
Expected Output: False
Explanation: A lone decimal point has no digit — invalid.
Input: ('',)
Expected Output: False
Explanation: Empty string is not a valid number.
Input: ('12 3',)
Expected Output: False
Explanation: Embedded space is not allowed anywhere.
Input: ('1,000',)
Expected Output: False
Explanation: Comma grouping separators are not allowed.
Input: ('5E+10',)
Expected Output: True
Explanation: Uppercase 'E' with a signed exponent and digits — valid.
Input: ('00.00',)
Expected Output: True
Explanation: Leading zeros are permitted on both sides of the decimal point.
Input: ('1e2e3',)
Expected Output: False
Explanation: Only one exponent is allowed; a second 'e' leaves a leftover character.
Hints
- Walk the string with a single index pointer and a small set of boolean flags (saw a digit? saw a dot?). This avoids the brittle corner cases of writing a regex.
- Split the parse into three phases: optional sign, mantissa, optional exponent. Each phase advances the index only over characters it legally accepts.
- The mantissa must contain at least one digit (so '.' alone is invalid) and at most one decimal point. The exponent, if present, must contain at least one digit after its optional sign.
- After parsing all phases, the answer is valid only if the index reached the end of the string — any leftover character (like a second '.', a trailing 'e', or an embedded space) means false.