Validate Numeric Price Literals in Bulk Seller Upload Files

Last updated: November 29, 2026

Quick Overview

Write `is_valid_number(s)` that accepts decimal literals such as `19.99`, `-0.5`, `.5`, `3.`, `2e10`, and `+6.02E-3`, and rejects strings like `e9`, `1e`, `.`, `--5`, or `4e3.5`, without using float() or regular expressions.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Hard

0

0

0 solved


Write `is_valid_number(s)` that accepts decimal literals such as `19.99`, `-0.5`, `.5`, `3.`, `2e10`, and `+6.02E-3`, and rejects strings like `e9`, `1e`, `.`, `--5`, or `4e3.5`, without using float() or regular expressions.

A harder online assessment style question in Amazon's style, framed around a catalog upload service that must reject malformed price and dimension fields before they corrupt listings. It is a state-machine exercise: the grader checks that you enumerate the grammar first and then handle every boundary without a tangle of flags.

What the Interviewer Expects
  • Writes down the grammar before coding
  • Tracks seen-digit, seen-dot, and seen-exponent state explicitly
  • Requires at least one digit in the mantissa and in the exponent
  • Allows a sign only at the start or right after e/E
  • Rejects a dot after the exponent
  • Tests tricky cases like '.', '3.', '+.e1'
Key Topics to Cover
string parsing
finite state machine
validation
edge cases
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you also accept thousands separators like 1,299.00 for US locales?
  • How would you parse the validated string into an exact decimal for currency?
  • How would you report which character made the input invalid?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Grammar, after trimming outer whitespace:

number := sign? mantissa (('e'|'E') sign? digits)?

mantissa := digits | digits '.' digits? | '.' digits

So the mantissa needs at least one digit, the e...

High-Level Design

Scan once and keep four facts: whether any digit has been seen (mantissa so far), whether a dot has been seen, whether an exponent has been seen, and whether at least one digit followed the exponent. ...


Submit Your Answer
Markdown supported