← Back to Stage 2
Stage 2#221Coding · Mid~10 min read

Fix a parseFloat money bug

React + TypeScript Interview Core · Money + Async Utilities

Convert decimal input into minor units with validation, precision limits, and clear user-facing errors.

Prompt

Fix a parseFloat money bug

This is a hands-on rep. Attempt the drill before reading the model answer, then narrate the tradeoffs as if an interviewer is watching.

💡
Tip

Money UI should not rely on binary floating point.

Foundation

What to ground before answering

Convert decimal input into minor units with validation, precision limits, and clear user-facing errors.

Focus vocabulary: money, precision, forms.

The useful mental model is not to memorize a perfect answer. It is to explain what owns the data, what can fail, what the user sees, and what test would prove the behavior.

Coding drill

Parse decimal money into minor units

Target: 20mNot run

Make the behavior executable before comparing against the model answer.

type AmountResult = { ok: true; minor: string } | { ok: false; error: string };

function parseAmountMinor(input: string, decimals: number): AmountResult {
  // TODO: parse without floating point. Return minor units as a base-10 string.
  return { ok: false, error: 'not implemented' };
}
TypeScript · runnable
System design

Interview explanation prompt

  • What problem is this practice item really testing?
  • What state or contract boundary must be explicit?
  • What edge case would cause a production regression?
  • What would you test first?
  • How would you explain the tradeoff in two minutes?
Self-grade

Self-grade

  • Strong answer handles the edge cases before polishing syntax.
  • Strong answer explains why the chosen type or function boundary prevents bugs.
  • Weak answer passes only the happy path or hides uncertainty in booleans and nullable fields.

Model Answer

function parseAmountMinor(input: string, decimals: number): AmountResult {
  const trimmed = input.trim();
  if (!/^\\d+(?:\\.\\d+)?$/.test(trimmed)) return { ok: false, error: 'invalid format' };
  const [whole, fraction = ''] = trimmed.split('.');
  if (fraction.length > decimals) return { ok: false, error: 'too many decimals' };
  const minor = whole + fraction.padEnd(decimals, '0');
  const normalized = minor.replace(/^0+(?=\\d)/, '');
  return { ok: true, minor: normalized };
}
Review

Recall before moving on

  • What is the one-sentence answer for "Fix a parseFloat money bug"?
  • Which real experience from PR TIMES, React/TypeScript migration, or systems work supports it?
  • What edge case would you volunteer before the interviewer asks?
  • What is the smallest test or artifact that proves the design works?