HardStringUpdated Sep 2026

65. Valid Number

Asked at LinkedIn

Difficulty

Hard

Time

O(n)

Space

O(1)

Pattern

String

Companies

1

Problem

You are given a string s. Decide whether it is a valid number in the usual programming-language sense: an optional sign, then either an integer or a decimal, and then optionally an exponent part.

A valid number has this shape: an optional + or -, then a mantissa, then optionally e or E followed by an exponent. The mantissa is either plain digits (an integer) or digits with exactly one dot (a decimal). The exponent is an optional sign followed by digits — no dot is allowed there.

Some rules that trip people up: a dot alone (".") is not a number, and a dot must have at least one digit somewhere on one of its sides ("4." and ".9" are fine). The e part must contain at least one digit, so "1e" and "e3" fail. A sign is only allowed at the very start or right after the e/E, so "--6" and "1e+2+3" fail. Any letter other than e/E makes the string invalid.

The shape of a valid number

optional sign + / -mantissa: digits, at most one dot, at least optional e / E + optional sign + digitsEvery valid number fits this template; anything thatdoes not is rejected.-90E301234"-90E3" fits the template: sign, mantissa digits,exponent digits.
A number is an optional sign, a mantissa with at most one dot and at least one digit, then an optional exponent with digits only.

Constraints

  • ●1 <= s.length <= 20
  • ●s contains only English letters, digits, '+', '-', and '.'

Examples

Example 1
Inputs = "0"
Outputtrue
WhyA single digit is a plain integer, which is a valid number.
Example 2
Inputs = "e"
Outputfalse
WhyThere is no mantissa before the e, and no digits after it either, so it is not a number.
Example 3
Inputs = "."
Outputfalse
WhyA dot with no digits on either side is not a number.

Asked At

1 companies
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).

One scan with three flags

-90E301234iStart: seenDigit = seenDot = seenExp = false. '-' is fine atindex 0.-90E301234iDigits 9 and 0 set seenDigit = true; no dot seen yet.-90E301234i'E' is allowed because a digit came before and no exponentyet; reset seenDigit = false.digit -> seenDigit = truesign -> only at i = 0 or after e/Edot -> fail if seenDot or seenExpe/E -> fail if seenExp or not seenDigit; reset seenDigit
Each character is legal only if the flags allow it at that position; the final seenDigit flag decides the answer.
ComplexityTime O(n)Space O(1)We scan the string once and only keep three boolean flags, so the cost is linear in the length with constant extra memory.

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

O(n) time
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 runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

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`.