Validate a simplified numeric string
MetaSoftware Engineer · Technical ScreenLast updated
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:
- 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.
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.