← Questions
Add two decimal number strings
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's technical phone screen, last asked Jul 2025. It tests strings and math.
Add two non-negative numbers given as strings and return their sum as a string, without converting them to built-in big integers or decimals. Start with whole numbers, then support a decimal point.
Part 1: whole numbers
Unless you agree on a different interface with the interviewer, assume:
-
add_strings(a: str, b: str) -> str— both are digit strings; add from the right with a carry.
add_strings("11", "123") -> "134"
add_strings("999", "1") -> "1000"
Part 2: decimal numbers
-
add_decimals(a: str, b: str) -> str— each input matches\d+(\.\d+)?: digits, optionally a point and more digits. Return the sum in canonical form: no leading zeros in the whole part (except a single0), no trailing zeros in the fractional part, and no point when the fractional part is empty.
add_decimals("1.25", "2.75") -> "4"
add_decimals("0.5", "9.75") -> "10.25"
add_decimals("007", "1.10") -> "8.1"
add_decimals("999.9", "0.1") -> "1000"
Constraints
-
1 <= len(a), len(b) <= 100,000. - No sign, no thousands separators, no exponent.
Clarifying questions
- Can inputs have leading zeros, or a bare point such as
".5"or"5."? - What exact output format do you want: canonical, or keep trailing zeros?
- Can numbers be negative?
What a strong answer covers
- Split each number at the point, pad the fractional parts on the right with zeros to the same length, pad the whole parts on the left.
- One right-to-left pass with a carry across the point, building digits into a list and reversing once.
- Normalising the result: strip leading zeros, strip trailing fractional zeros, drop the point if nothing is left after it.
-
O(max(len(a), len(b)))time and space. - Tests: carry across the point, carry out of the top digit, one input without a point, results that are whole numbers.
Follow-ups
- Subtract two decimal strings, including a negative result.
- Support a leading
-sign on either input. - Multiply two decimal strings.
Where it's asked
- Meta · Technical phone screen · Last asked Jul 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
