Add Decimal Strings Exactly
Updated
Problem statement
You are given two decimal numbers as strings a and b. Return their exact sum as a decimal string. Do not use eval, and do not convert either input to a floating-point number before adding.
Both inputs are nonnegative. Each has one or more digits before an optional decimal point. If a decimal point is present, one or more digits follow it. Inputs never contain signs, exponent notation, or whitespace. Leading and trailing zeroes are allowed.
Write the result in its shortest form. Remove extra leading zeroes from the whole-number part and extra trailing zeroes from the fraction part. If no fraction digits are left, leave out the decimal point. For values below one, keep a single zero before the point. If the sum is exactly zero, return 0.
Function
addDecimal(a: String, b: String) → StringExamples
Example 1
a = "1.00000000012"b = "22222.1"return = "22223.10000000012"This example needs all eleven fractional places to remain exact.
Example 2
a = "009.0090"b = "00.991"return = "10"The fraction parts add up to exactly one whole, so a carry moves into the whole-number part. Then the extra zeroes are removed, and the decimal point is dropped because no fraction digits remain.
Example 3
a = "0.001"b = "7"return = "7.001"An integer input has no fraction part, so treat that part as zero before adding.
Constraints
1 <= a.length,b.length <= 10000.- Each input follows the nonnegative decimal format described above.
- The sum must stay exact, even when it does not fit in any fixed-width integer or floating-point type.
More Meta Phone Screen questions
- Convert a Sorted Linked List to a Balanced BSTPHONE SCREEN · Seen Nov 2026
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Iterate Canonical IPv6 AddressesOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Nth Staged License PlateGoogle · ONSITE INTERVIEWGoogle interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
- Format a Centered Pagination BarMixpanel · PHONE SCREENMixpanel interview questions
- Multiply StringsZillow · PHONE SCREENZillow interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Add Two Integers Given as StringsJane Street · PHONE SCREENJane Street interview questions
- Fizz and Buzz Words From 1 to nDocuSign · PHONE SCREENDocuSign interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences