PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string-parsing, formal-grammar validation, and edge-case reasoning competencies, including the ability to recognize valid numeric formats and to provide time and space complexity analysis.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Validate a simplified numeric string

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Implement isValidNumber (s) that returns true if and only if the ASCII string s represents a valid number under these simplified rules: - Optional leading '+' or '-'. - A mantissa that is either: (a) one or more digits, optionally containing a single decimal point anywhere (e.g., '123', '0.5', '.8', '10.'), with at least one digit overall; or (b) exactly a decimal point is not allowed ('.' invalid). - An optional exponent part: 'e' or 'E' followed by an optional '+' or '-' and then at least one digit (e.g., '1e9', '-3.2E-4'). - No spaces, commas, underscores, or other characters are allowed anywhere. - Leading zeros are allowed; multiple signs or multiple decimal points are not. Return false for cases like 'e3', '+.', '1.2.3', '--6', '6e', '6e-'. Provide time and space complexities and briefly justify your approach.

Quick Answer: This question evaluates string-parsing, formal-grammar validation, and edge-case reasoning competencies, including the ability to recognize valid numeric formats and to provide time and space complexity analysis.

Implement `isValidNumber(s)` that returns `true` if and only if the ASCII string `s` represents a valid number under these simplified rules: - An optional leading `'+'` or `'-'` sign. - A mantissa that is one or more digits, optionally containing a **single** decimal point placed anywhere, with **at least one digit overall**. Valid mantissas include `'123'`, `'0.5'`, `'.8'`, and `'10.'`. A lone `'.'` is **not** valid. - An optional exponent part: `'e'` or `'E'`, followed by an optional `'+'` or `'-'`, then **at least one digit** (e.g., `'1e9'`, `'-3.2E-4'`). - No spaces, commas, underscores, or any other characters are allowed anywhere. - Leading zeros are allowed; multiple signs or multiple decimal points are not. Return `false` for cases like `'e3'`, `'+.'`, `'1.2.3'`, `'--6'`, `'6e'`, and `'6e-'`. **Approach:** Scan the string left to right with a single index. Consume an optional leading sign, then the mantissa (digits and at most one dot, requiring at least one digit), then an optional exponent (`e`/`E`, optional sign, at least one digit). The string is valid only if the scan consumes every character. This single linear pass avoids the complexity and corner-case pitfalls of a regex while remaining easy to reason about.

Constraints

  • 0 <= len(s) <= 10^5
  • s consists of printable ASCII characters; any character outside the allowed set (sign, digit, '.', 'e'/'E') makes s invalid.
  • An empty string is invalid.
  • At most one decimal point and at most one exponent are permitted.

Examples

Input: ('123',)

Expected Output: True

Explanation: Plain integer with no sign, dot, or exponent — valid.

Input: ('0.5',)

Expected Output: True

Explanation: Single decimal point between digits — valid mantissa.

Input: ('.8',)

Expected Output: True

Explanation: Decimal point may lead the mantissa as long as a digit follows.

Input: ('10.',)

Expected Output: True

Explanation: A trailing decimal point is allowed because at least one digit precedes it.

Input: ('1e9',)

Expected Output: True

Explanation: Exponent 'e' followed directly by a digit — valid.

Input: ('-3.2E-4',)

Expected Output: True

Explanation: Leading sign, decimal mantissa, and a signed exponent — all valid together.

Input: ('+6',)

Expected Output: True

Explanation: Optional leading '+' sign followed by a digit — valid.

Input: ('e3',)

Expected Output: False

Explanation: Exponent with no mantissa digit before it — invalid.

Input: ('+.',)

Expected Output: False

Explanation: Sign and a lone dot with no digit — mantissa has no digit, invalid.

Input: ('1.2.3',)

Expected Output: False

Explanation: Two decimal points are not allowed.

Input: ('--6',)

Expected Output: False

Explanation: Multiple leading signs are not allowed.

Input: ('6e',)

Expected Output: False

Explanation: Exponent marker with no digit following it — invalid.

Input: ('6e-',)

Expected Output: False

Explanation: Exponent has a sign but no digit after it — invalid.

Input: ('.',)

Expected Output: False

Explanation: A lone decimal point has no digit — invalid.

Input: ('',)

Expected Output: False

Explanation: Empty string is not a valid number.

Input: ('12 3',)

Expected Output: False

Explanation: Embedded space is not allowed anywhere.

Input: ('1,000',)

Expected Output: False

Explanation: Comma grouping separators are not allowed.

Input: ('5E+10',)

Expected Output: True

Explanation: Uppercase 'E' with a signed exponent and digits — valid.

Input: ('00.00',)

Expected Output: True

Explanation: Leading zeros are permitted on both sides of the decimal point.

Input: ('1e2e3',)

Expected Output: False

Explanation: Only one exponent is allowed; a second 'e' leaves a leftover character.

Hints

  1. Walk the string with a single index pointer and a small set of boolean flags (saw a digit? saw a dot?). This avoids the brittle corner cases of writing a regex.
  2. Split the parse into three phases: optional sign, mantissa, optional exponent. Each phase advances the index only over characters it legally accepts.
  3. The mantissa must contain at least one digit (so '.' alone is invalid) and at most one decimal point. The exponent, if present, must contain at least one digit after its optional sign.
  4. After parsing all phases, the answer is valid only if the index reached the end of the string — any leftover character (like a second '.', a trailing 'e', or an embedded space) means false.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)