Skip to main content

95. Valid Number

hardAsked at Snowflake

Decide whether a string parses as a number — integer, decimal, or scientific notation — under a spec deliberately packed with corner cases ('.', '+', '1e', '.e10'). Snowflake candidates report this hard because a data-warehouse SQL parser must tokenize numeric literals exactly, and the question filters for engineers who reach for an explicit state machine instead of a clever regex they cannot defend character by character.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake compiler-team uses this to test edge-case rigor.
  • LeetCode Discuss (2025-08)Reported at Snowflake SDE-II screens.

Problem

A valid number can be split into these components (in order): a decimal number or an integer, optionally an 'e' or 'E' followed by an integer. A decimal number has an optional sign and one of: digits followed by '.' optional digits; '.' followed by digits; digits only. An integer has an optional sign followed by digits. Given a string s, return true if s is a valid number.

Constraints

  • 1 <= s.length <= 20
  • s consists of only English letters (both uppercase and lowercase), digits (0-9), plus '+', minus '-', or dot '.'.
  • No whitespace appears in s — older versions of this problem allowed leading/trailing spaces; the current spec does not.

Examples

Example 1

Input
s = "0"
Output
true

Explanation: A single digit is the simplest valid number — seenDigit flips true and no other rule is violated.

Example 2

Input
s = "e"
Output
false

Explanation: An exponent marker with no mantissa before it and no digits after it fails both digit requirements.

Example 3

Input
s = "."
Output
false

Explanation: A dot needs at least one digit on one side — bare '.' has none, so seenDigit never becomes true.

Example 4

Input
s = "2e10"
Output
true

Explanation: Digits, then 'e', then integer exponent — the full scientific-notation path through the state machine.

Approaches

1. Heavy regex

Write a regex that captures all cases.

Time
O(n)
Space
O(n)
function isNumber(s) {
  return /^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(s);
}

Tradeoff: Cute but no interviewer accepts a regex one-liner — they want to see the state machine.

2. Finite state machine (optimal)

Track flags: seenDigit, seenDot, seenE, seenDigitAfterE. Walk the string updating flags; reject invalid transitions.

Time
O(n)
Space
O(1)
function isNumber(s) {
  let seenDigit = false, seenDot = false, seenE = false, seenDigitAfterE = true;
  for (let i = 0; i < s.length; i++) {
    const c = s[i];
    if (c >= '0' && c <= '9') {
      seenDigit = true;
      seenDigitAfterE = true;
    } else if (c === '+' || c === '-') {
      if (i > 0 && s[i-1] !== 'e' && s[i-1] !== 'E') return false;
    } else if (c === '.') {
      if (seenDot || seenE) return false;
      seenDot = true;
    } else if (c === 'e' || c === 'E') {
      if (seenE || !seenDigit) return false;
      seenE = true;
      seenDigitAfterE = false;
    } else {
      return false;
    }
  }
  return seenDigit && seenDigitAfterE;
}

Tradeoff: Explicit state-tracking. Easy to extend (e.g., for hex literals).

Snowflake-specific tips

Snowflake candidates report interviewers steering away from regex one-liners toward the flag-based state machine, then probing edge cases one at a time: '+.8'? '3.'? '.e1'? '46.e3'? Answer from the flags, not from memory — 'seenDigit is still false when we hit e, so reject' is the grading currency. The strongest extension answer connects it to real tokenizer work: a SQL dialect adds hex (0x...), binary, and underscore digit separators, and the FSM grows new states cleanly where a regex would need a rewrite.

Common mistakes

  • Allowing '.' after 'e' (scientific notation requires integer exponent).
  • Treating '+' or '-' at any position as valid — must follow start, 'e', or 'E'.
  • Forgetting that at least one digit must follow 'e'.

Follow-up questions

An interviewer at Snowflake may pivot to one of these next:

  • Parse Number (return the actual value).
  • Hex / binary / octal literal support.
  • How does Snowflake's parser tokenize numbers?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why a state machine over regex?

FSM is easier to extend, easier to debug edge cases, and what production tokenizers actually use. Regex is a black box that hides decisions.

What's the trickiest edge case?

'.' alone (false), '+' alone (false), '.e10' (false — must have a digit), '1e' (false — exponent missing). These trip up shallow regexes.

Why initialize seenDigitAfterE to true?

Until an 'e' appears there is no exponent to satisfy, so the flag must not block acceptance of plain numbers like '42'. Setting it false only when 'e' is consumed makes the final return seenDigit && seenDigitAfterE correct for both notations.

Companies that also ask Valid Number