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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

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
String Arithmetic
Carry Propagation
Decimal Precision
Normalization
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: two strings matching digits[.digits], where either side of the dot may be empty but not both (.25 and 3. are fine).
  • Output: the exact sum, with no leading zeros (except a single 0 b...
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...


Submit Your Answer
Markdown supported