</> 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: Walk a value→symbol table from largest to smallest, and at each step grab as many copies of the biggest symbol that still fits.

Problem, rephrased

You're building a small typesetting feature for a museum's exhibit plaques. The curators give you a plain integer like 1994, and your job is to render it the way the engravers want it: as a Roman numeral string, "MCMXCIV".

Roman numerals use seven base symbols:

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

Most of the time you just write symbols largest-to-smallest and add them up: XII is 10 + 1 + 1 = 12. But six special cases use subtraction β€” a smaller symbol placed in front of a larger one means "subtract": IV is 4 (not IIII), IX is 9, XL is 40, and so on.

Your task: given an integer num in the range 1 .. 3999, return its Roman numeral string.

Input Output Why
3 "III" three ones
58 "LVIII" 50 + 5 + 1 + 1 + 1
1994 "MCMXCIV" 1000 + 900 + 90 + 4

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.