Check Whether a String Is a Signed Decimal Number Without Exponent Notation
Company: LinkedIn
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Write a function that decides whether a string is a valid plain decimal number with an optional sign and fractional part, rejecting exponents, stray symbols, and repeated decimal points. It tests turning a small grammar into careful character-by-character validation and covering edge cases such as empty strings and bare signs.
Constraints
- 0 <= len(to_test) <= 100.
- to_test contains only printable ASCII characters (codes 32 through 126), including spaces.
- Valid strings use only the characters '+', '-', '.', and the decimal digits '0' through '9'; any other character makes the string invalid rather than being an error.
- The function returns a boolean and performs no input/output.
Examples
Input: ('',)
Expected Output: False
Explanation: The empty string has no digits, and at least one digit is required.
Input: ('7',)
Expected Output: True
Explanation: A single digit with no sign and no fractional part is the smallest valid number.
Hints
- The form has exactly three parts in a fixed order: an optional sign, an integer part, and an optional fractional part. Work out where each part ends before deciding anything.
- Both digit runs must be non-empty when their part is present. Ask yourself which of those two requirements each of "", "-", ".5", and "5." violates.
- Validity is about the whole string: anything left over after the form has been matched, whether a second '.', a trailing sign, or a space, makes the answer False.