Hard
String
Updated Sep 2026

Valid Number

Asked at LinkedIn

Problem

Valid Number asks whether a string represents a legal decimal or scientific-notation number, such as "2", "-0.1", "4.", ".5", or "-90E3", while rejecting things like "e3", "99e2.5", "--6", or ".". It is rated Hard not because of algorithms but because of edge cases — the interviewer wants to see you define the grammar precisely and encode it without a pile of special cases.

Asked At

CompanyDifficulty
LinkedInHardView all LinkedIn questions →

How to Think About It

1.

Write down the grammar first. A valid number is: optional sign, then digits with at most one dot and at least one digit, then optionally e/E followed by an optional sign and at least one digit (no dot).

2.

Key insight: a single left-to-right scan with three flags handles every case — seenDigit, seenDot, and seenExp. Each character is only legal if the flags say it is allowed at this point.

3.

Rules per character: a digit sets seenDigit. A sign is only legal at index 0 or right after an e. A dot is illegal after a previous dot or after an exponent. An e is illegal if you already saw one or if no digit came before it — and after it you reset seenDigit = false so the exponent must have its own digits.

4.

Walkthrough for "-90E3": - at index 0 OK; 9,0 set seenDigit; E allowed (digit seen, no exp yet) -> seenExp = true, seenDigit = false; 3 sets seenDigit. End: seenDigit is true -> valid.
Walkthrough for "1e": after e, seenDigit was reset and nothing follows -> invalid.

5.

Edge cases worth testing aloud: ".", "+.", ".e1", "4e+", "+-5", "95a54e53", "0089" (valid), "-.9" (valid).

Optimal Approach

Step 1: Initialize seenDigit = seenDot = seenExp = false.
Step 2: For each index i and char ch:
If digit: seenDigit = true.
If + or -: valid only if i == 0 or s[i-1] is e/E.
If .: invalid if seenDot or seenExp; otherwise seenDot = true.
If e/E: invalid if seenExp or not seenDigit; otherwise seenExp = true and seenDigit = false.
Anything else: invalid.
Step 3: Return seenDigit.

The final seenDigit check covers both "no digits at all" and "exponent with no digits".

Time: O(n). Space: O(1).

What Trips People Up in Real Interviews

1.

Using float(s) or a language parser. Those accept inputs like "inf", "nan", or "1_000" that are invalid here — and the interviewer wants the logic, not a library call.

2.

Forgetting to reset seenDigit after the exponent. Without it, "1e" is accepted because a digit was seen before the e.

3.

Allowing a dot inside the exponent. "99e2.5" must be rejected — dots are only legal before the e.

4.

Diving straight into code. On this problem the grammar is the solution; state it in one sentence, then implement it with flags. It is much easier to defend than 20 nested ifs.

5.

Accepting a sign in the middle, like "6+1". A sign is only legal at position 0 or right after e/E.

Solution Code

def isNumber(s):
    seen_digit = seen_dot = seen_exp = False
    for i, ch in enumerate(s):
        if ch.isdigit():
            seen_digit = True
        elif ch in '+-':
            if i > 0 and s[i - 1] not in 'eE':
                return False
        elif ch == '.':
            if seen_dot or seen_exp:
                return False
            seen_dot = True
        elif ch in 'eE':
            if seen_exp or not seen_digit:
                return False
            seen_exp = True
            seen_digit = False
        else:
            return False
    return seen_digit

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Valid Number problem?

Valid Number asks whether a string represents a legal decimal or scientific-notation number, such as `"2"`, `"-0.1"`, `"4."`, `".5"`, or `"-90E3"`, while rejecting things like `"e3"`, `"99e2.5"`, `"--6"`, or `"."`. It is rated Hard not because of algorithms but because of edge cases — the interviewer wants to see you define the grammar precisely and encode it without a pile of special cases.

How do you solve Valid Number?

The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.

What companies ask Valid Number?

Valid Number is asked at LinkedIn. It is a hard difficulty problem.

What are common mistakes on Valid Number?
  • Using `float(s)` or a language parser. Those accept inputs like `"inf"`, `"nan"`, or `"1_000"` that are invalid here — and the interviewer wants the logic, not a library call.
  • Forgetting to reset `seenDigit` after the exponent. Without it, `"1e"` is accepted because a digit was seen before the `e`.
  • Allowing a dot inside the exponent. `"99e2.5"` must be rejected — dots are only legal before the `e`.
  • Diving straight into code. On this problem the grammar is the solution; state it in one sentence, then implement it with flags. It is much easier to defend than 20 nested ifs.
  • Accepting a sign in the middle, like `"6+1"`. A sign is only legal at position 0 or right after `e`/`E`.