Skip to main content

95. Valid Number

hardAsked at Snowflake

Determine whether a string is a valid number (integer, decimal, or scientific notation). Snowflake asks this because their SQL parser must tokenize numeric literals correctly — and the spec is full of edge cases.

By Alex Chen, 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 '.'.

Examples

Example 1

Input
s = "0"
Output
true

Example 2

Input
s = "e"
Output
false

Example 3

Input
s = "."
Output
false

Example 4

Input
s = "2e10"
Output
true

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 interviewers want the FSM-style implementation. Bonus signal: discuss how their SQL parser tokenizes numeric literals — similar FSM with extensions for hex (0x...), binary (0b...), and underscore separators in big numbers.

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.

Practice these live with InterviewChamp.AI

Drill Valid Number and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →