Add Two Decimal Money Strings Digit by Digit Without Floats
Last updated: November 29, 2026
Quick Overview
Given two non-negative decimal numbers as strings, possibly with different numbers of fractional digits (for example "12.5" and "0.75"), return their exact sum as a canonical string without converting the inputs to numeric types.
0
0
0 solved
Given two non-negative decimal numbers as strings, possibly with different numbers of fractional digits (for example "12.5" and "0.75"), return their exact sum as a canonical string without converting the inputs to numeric types.
Google Pay and Cloud Billing never trust binary floating point with money, which motivates this phone screen problem. The interviewer is checking alignment of the decimal point, carry propagation across the point, and canonical output with no stray zeros or dangling dots.
What the Interviewer Expects
- Explains why floats give wrong answers such as 0.1 + 0.2
- Aligns the fractional parts by right-padding and the whole parts by left-padding
- Adds digit by digit from the right with a carry that crosses the decimal point
- Strips leading zeros in the whole part and trailing zeros in the fraction
- Validates malformed input
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Support negative numbers, which turns some additions into subtractions.
- Multiply two decimal strings exactly.
- Round the result to two decimals using banker's rounding.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: two strings matching
digits[.digits], where either side of the dot may be empty but not both (.25and3.are fine). - Output: the exact sum, with no leading zeros (except a single
0b...
High-Level Design
Split each number at the dot. Let scale be the longer fraction length and width the longer whole length. Left-pad the whole parts and right-pad the fractions to those sizes and concatenate, giving...