Skip to main content

94. Valid Number

hardAsked at Salesforce

Determine if a string represents a valid number. Salesforce uses this to test edge-case enumeration and finite-state-machine reasoning.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses similar validation for numeric field inputs in custom Apex.
  • LeetCode Discuss (2025)Hard for the edge-case enumeration.

Problem

Given a string s, return whether s is a valid number. A valid number can be split into these components: a decimal number (followed optionally by 'e' or 'E' and an integer), or an integer (followed optionally by 'e' or 'E' and an integer). A decimal/integer can be signed. See LeetCode for the full grammar.

Constraints

  • 1 <= s.length <= 20
  • s consists of only English letters, 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. Regex

Match against a carefully crafted regex.

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

Tradeoff: Works but Salesforce may forbid regex — they want to see FSM reasoning.

2. Finite State Machine

Track states: start, sign, integer, dot, fraction, exp, expSign, expInt. Transitions on each character.

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 FSM. Each transition is enforced via boolean flags. Salesforce's preferred answer.

Salesforce-specific tips

Salesforce uses similar validation for numeric Apex field inputs (Currency, Number with decimal places). They grade on whether you can enumerate all the edge cases (leading +/-, '.', 'e', '+/-' after e, trailing digits after e). Bonus signal: mention this is essentially a regular language — the regex is correct precisely because no infinite recursion is needed.

Common mistakes

  • Allowing '+' or '-' anywhere (not just start or after e/E).
  • Allowing '.' after 'e' — exponent must be integer.
  • Allowing 'e' without digits before it — '.e1' is invalid.

Follow-up questions

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

  • Parse and convert to number (LC 8, atoi).
  • Validate scientific notation only.
  • Support hex and octal.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Is regex acceptable?

Depends on the interviewer. Some forbid regex to test FSM reasoning; others accept it. Always offer both if you have time.

What about '.5' and '5.'?

Both are valid — at least one digit (before OR after the dot) is required. The FSM tracks 'seenDigit' to ensure this.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →