Implement precise string-based decimal addition
MixpanelSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's proficiency in string-based arbitrary-precision arithmetic, including decimal alignment, fractional carry propagation, normalization of numeric strings, and analysis of time and space complexity.
Given two non-empty strings representing decimal numbers (e.g., "123.456" and "6234.42432"), implement a function that returns their exact sum as a string without using floating-point arithmetic or big-number libraries and without precision loss. Inputs may be very large (up to 1e5 total digits) and may contain leading/trailing zeros and decimal points but no scientific notation. Describe how you align fractional parts, propagate carries across the decimal point, and normalize the output (e.g., removing unnecessary leading/trailing zeros while preserving a fractional zero when needed). State time and space complexity and provide tests for edge cases such as different fractional lengths, one operand lacking a fractional part, all-zero values, and very large inputs.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.