Validate a simplified numeric string
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Implement isValidNumber
(s) that returns true if and only if the ASCII string s represents a valid number under these simplified rules:
- Optional leading '+' or '-'.
- A mantissa that is either:
(a) one or more digits, optionally containing a single decimal point anywhere (e.g., '123', '0.5', '.8', '10.'), with at least one digit overall; or
(b) exactly a decimal point is not allowed ('.' invalid).
- An optional exponent part: 'e' or 'E' followed by an optional '+' or '-' and then at least one digit (e.g., '1e9', '-3.2E-4').
- No spaces, commas, underscores, or other characters are allowed anywhere.
- Leading zeros are allowed; multiple signs or multiple decimal points are not.
Return false for cases like 'e3', '+.', '1.2.3', '--6', '6e', '6e-'. Provide time and space complexities and briefly justify your approach.
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.
Implement `isValidNumber(s)` that returns `true` if and only if the ASCII string `s` represents a valid number under these simplified rules:
- An optional leading `'+'` or `'-'` sign.
- A mantissa that is one or more digits, optionally containing a **single** decimal point placed anywhere, with **at least one digit overall**. Valid mantissas include `'123'`, `'0.5'`, `'.8'`, and `'10.'`. A lone `'.'` is **not** valid.
- An optional exponent part: `'e'` or `'E'`, followed by an optional `'+'` or `'-'`, then **at least one digit** (e.g., `'1e9'`, `'-3.2E-4'`).
- No spaces, commas, underscores, or any other characters are allowed anywhere.
- Leading zeros are allowed; multiple signs or multiple decimal points are not.
Return `false` for cases like `'e3'`, `'+.'`, `'1.2.3'`, `'--6'`, `'6e'`, and `'6e-'`.
**Approach:** Scan the string left to right with a single index. Consume an optional leading sign, then the mantissa (digits and at most one dot, requiring at least one digit), then an optional exponent (`e`/`E`, optional sign, at least one digit). The string is valid only if the scan consumes every character. This single linear pass avoids the complexity and corner-case pitfalls of a regex while remaining easy to reason about.
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.
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.