Prepin
Log in

How can you add two strings, each containing a number greater than 32 bits, and return the result as a string?

Approach / Explanation

This question requires you to figure out how to add two extremely large numbers that surpass the capacity of a 32-bit integer. Additionally, these numbers are provided as strings rather than integers. Consequently, standard addition methods cannot be applied directly due to their string representation. Your objective is to devise a method to carry out this addition and ultimately return the result as a string. To effectively address this question, keep the following points in mind: The fact that these numbers exceed 32-bit indicates that they surpass the typical integer (int) limits found in most programming languages. Therefore, traditional addition operations are not applicable, and an alternative approach is necessary. Since the numbers are represented as strings, you must discover a way to execute arithmetic operations such as addition on them. The result must also be formatted as a string. Additionally, consider the method of adding numbers digit by digit from the right, which is the way addition is typically performed by humans.

Suggested Answer

A straightforward method to tackle this issue might involve transforming the strings into BigIntegers (which are supported in some programming languages, such as Java) and performing the addition. However, let's approach the problem without resorting to such conversions. A more effective method would be to apply the traditional technique of addition, which involves starting from the least significant digit and progressing toward the most significant one. Below is a Python code example that demonstrates how to implement this: def addStrings ( num1 , num2 ) : i , j , carry , res = len ( num1 ) - 1 , len ( num2 ) - 1 , 0 , [ ] while i >= 0 or j >= 0 or carry : n1 = int ( num1 [ i ] ) if i >= 0 else 0 n2 = int ( num2 [ j ] ) if j >= 0 else 0 carry , curr = divmod ( n1 + n2 + carry , 10 ) res . append ( str ( curr ) ) i , j = i - 1 , j - 1 return '' . join ( res [ : : - 1 ] ) We start by positioning pointers i and j at the ends of num1 and num2 , respectively, decrementing them by one at each step (moving left). We also keep track of the carry from the previous addition. At each iteration, we compute the sum of the current digits and the carry, storing the last digit of this sum in res . The remaining portion becomes the carry for the subsequent step. This process continues until all digits of the numbers and the carry have been processed.

Alternative Answer

In Java, we can approach this problem in a similar manner. We traverse the input strings from right to left, convert each character to a number by subtracting '0' , and sum them up. If the total exceeds 9, we carry over 1 to the next digit. Ultimately, we concatenate each calculated digit and return the final sum as a string. The following demonstrates how to implement this in Java: public String addStrings ( String num1 , String num2 ) { int i = num1 . length ( ) - 1 , j = num2 . length ( ) - 1 , carry = 0 ; StringBuilder sb = new StringBuilder ( ) ; while ( i >= 0 || j >= 0 ) { int n1 = i >= 0 ? num1 . charAt ( i ) - '0' : 0 ; int n2 = j >= 0 ? num2 . charAt ( j ) - '0' : 0 ; int tmp = n1 + n2 + carry ; carry = tmp / 10 ; sb . append ( tmp % 10 ) ; i -- ; j -- ; } if ( carry == 1 ) sb . append ( 1 ) ; return sb . reverse ( ) . toString ( ) ; } In this implementation, the StringBuilder is employed to append each calculated digit. If there is a carry after the loop completes, we include that in our string. Finally, we reverse the string and return the outcome.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Waymo

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.