</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Numerals and Parsing

What is this?

This is about translating numbers to and from the ways humans write them โ€” Roman numerals like MCMXCIV, the English words "one thousand two hundred", or a decimal you read off a string. Each notation is really just a small dictionary of symbols plus rules for how they combine. Parsing also means reading messy real-world text and pulling a clean number out of it.

flowchart TD A["Human notation"] --> B["Match largest piece in the table"] B --> C["Emit symbol and reduce the remainder"] C --> D["Handle signs spaces and overflow"] D --> E["Final number or string"]

๐Ÿ’ก Fun fact: A repeating decimal like 1/7 = 0.142857142857... never actually ends, but you can detect the loop in code by remembering each remainder you have already seen โ€” the moment one repeats, you have found the cycle.

๐Ÿ”“ The 5 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: human number notations are just lookup tables plus rules for grouping and ordering, so converting them is a matter of greedily matching the largest piece, walking a remainder, and guarding the messy edge cases.

The pattern

Each problem here pairs a fixed vocabulary with a strategy for consuming or producing it. The recurring moves are: keep a value table sorted from largest to smallest and greedily subtract or emit the biggest piece that fits; group digits into fixed-size chunks so a small rule set covers an unbounded range; and run a state machine when raw text has optional signs, whitespace, or trailing junk.

The reason greedy works for these notations is that they are designed to be unambiguous from the top down โ€” the largest applicable symbol is always part of a valid representation. The reason parsing is fiddly is the opposite: real input is messy, and the hard work is rejecting or clamping what does not fit rather than the conversion itself. Long division adds a third idea โ€” track remainders to detect when a process repeats.

The problems

Integer to Roman uses a greedy value table that already includes the subtractive forms (4, 9, 40, 90, ...). Walk the table high to low, appending each symbol while it fits and subtracting its value, so the awkward cases need no special handling.

Roman to Integer scans left to right, adding each symbol's value but subtracting when a smaller symbol precedes a larger one โ€” that lone comparison captures every subtractive pair.

Integer to English Words groups the digits into threes (thousand, million, billion), names each group with one helper, and stitches the scale words between them โ€” turning an open-ended range into a tiny rule set.

Fraction to Recurring Decimal is long division: emit the integer part, then repeatedly multiply the remainder, recording each remainder in a map; the first repeat marks where the recurring block begins.

String to Integer (atoi) is a state machine: skip leading spaces, read an optional sign, consume digits, stop at the first non-digit, and clamp to the integer range on overflow.

Key takeaways

  • A largest-first value table makes Roman conversions a clean greedy loop in both directions.
  • Grouping digits into threes collapses arbitrarily large numbers into a handful of naming rules.
  • Long division detects repetition by remembering remainders; a repeated remainder is a repeating decimal.
  • Robust parsing is mostly edge-case discipline: signs, whitespace, trailing characters, and overflow clamping.

Core idea: Do long division by hand, and remember every remainder you've seen โ€” the decimal starts repeating the instant a remainder shows up a second time.

Problem, rephrased

You're building a tiny calculator that has to display fractions exactly, never as a rounded 0.16666667. Given a numerator and a denominator, produce the precise decimal string. If the digits after the point eventually repeat forever, you must show that honestly by wrapping the repeating block in parentheses.

So 1/6 is not "approximately 0.1667" โ€” it is exactly 0.1(6), meaning "0.1 then 6 repeating forever."

Three flavours of answer you must handle:

Numerator Denominator Output Why
1 8 "0.125" Terminating โ€” division comes out clean
2 3 "0.(6)" Pure repeating โ€” repeats from the first decimal
1 6 "0.1(6)" Mixed โ€” one settled digit, then a repeating tail
1 2 "0.5" Terminating
4 333 "0.(012)" Repeating block of length 3
4 2 "2" Integer result, no decimal point at all

You also need to get the sign right (-1/6 โ†’ "-0.1(6)") and handle the integer part (7/2 โ†’ "3.5").

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.