Validate Numeric Literals With an Explicit Finite State Machine
Last updated: November 29, 2026
Quick Overview
Decide whether a string is a valid decimal number literal, allowing an optional sign, digits with an optional decimal point, and an optional exponent part with its own sign, by implementing a finite state machine.
Microsoft
November 29, 20260
0
0 solved
Decide whether a string is a valid decimal number literal, allowing an optional sign, digits with an optional decimal point, and an optional exponent part with its own sign, by implementing a finite state machine.
An onsite question in the style of Microsoft teams that build parsers, such as the Excel formula engine or the C# and TypeScript compilers. The interviewer wants a principled state machine rather than a tangle of boolean flags, and will probe cases like ".", "1.", ".5" and "1e".
What the Interviewer Expects
- Enumerate valid and invalid examples before designing
- Define states and transitions by character class
- Mark accepting states explicitly
- Reject on any missing transition
- Explain how to extend the machine for new syntax
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
- How would you extend the machine to accept hex literals like 0x1F?
- How would you allow digit separators like 1_000_000 but not at the ends?
- How would you turn this recognizer into a tokenizer that returns the numeric value?
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, -12, +3.14, 1., .5, 2e10, -1E-7, 3.e+2. Invalid: empty, ., e5, 1e, 1e+, +-1, 1.2.3, 1e2.5, letters, and whitespace (assume the caller trims; confirm). The manti...
High-Level Design
Classify each character into digit, sign, dot or exponent marker (anything else has no class and fails). Then drive a table of states:
start,sign: before any digit.int: digits seen, no do...