Validate Numeric Literals in Config Files With a Finite State Machine
Last updated: November 29, 2026
Quick Overview
Write is_number(s) that decides whether a string is a valid decimal number: optional sign, digits with an optional decimal point, and an optional exponent part with its own sign, surrounded by optional whitespace.
ByteDance
November 29, 20260
0
0 solved
Write is_number(s) that decides whether a string is a valid decimal number: optional sign, digits with an optional decimal point, and an optional exponent part with its own sign, surrounded by optional whitespace.
Volcano Engine services load large YAML and JSON configs, and a hand-written validator for numeric fields is a typical ByteDance screen problem. The interviewer is less interested in regex tricks than in whether you can enumerate states and transitions cleanly, so that the code is easy to extend and obviously correct.
What the Interviewer Expects
- Model the grammar as explicit states and transitions
- Identify accepting states precisely
- Handle cases like '3.', '.5', and '+.e1' correctly
- Trim surrounding whitespace but reject inner spaces
- Explain why a table beats a pile of boolean flags
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Extend the machine to accept hexadecimal literals like 0x1F.
- How would you also support underscores as digit separators?
- Return the parsed value as well, without floating point surprises.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Valid: 0, 0.1, -90E3, 3., .5, +6e-1. Invalid: abc, 1e, e3, 99e2.5, --6, ., empty string, 1 2. At least one digit must appear before the exponent, and the exponent must be a...
High-Level Design
Classify each character as digit, sign, dot, exponent, or other. States: start, sign, int (digits seen), dot_no_int (a dot with no digits yet), frac (digits after or around a dot), exp, `e...