## Amazon Interview Question

Backend Developers**Country:**United States

this would be my answer, with a few holes of course...

```
private static String addNumbers(String[] no) // assumes all given strings are in decimal no format
{
String sum = "";
int bal = 0;
int max = 0;
for (String no1 : no) // max length of string nos
if (max < no1.length())
max = no1.length();
for (int count = 1; count <= max || bal > 0; count++)
{
for (String no1 : no) // \ find sum of individual digits
if (no1.length() - count >= 0) // --> assumes that the sum of individual
bal += (no1.charAt(no1.length() - count)-'0'); // / digits (bal) never exceeds int max limit
sum=""+(bal%10)+sum;
bal/=10;
}
return sum;
}
```

This is terribly language specific question. In fact I would go to the point saying it is a non question for anything above C. In C++ we already have arbitrary precision integers.

[ stackoverflow.com/questions/2568446/the-best-cross-platform-portable-arbitrary-precision-math-library ]

In a slightly better human/declarative language -- this is as cakewalk as :

`sum ( file( 'input.txt' ) ) as { int( $.o ) } // this is ZoomBA`

`sum(int(v) for v in open('input.txt') ) # Python`

Now, same in Java is actually left as exercise.

As @NoOne pointed out, the problem is extremely trivial if we take the assumption that we use the int() function. However, if we are not allowed to do so, then we have to add strings and use subtraction to get the integer value for each character in the string (this is similar to the atoi function approach). I have outlined both solutions below. I am making an assumption that we can still use string functions (since that's our input) and I use one another iterable function.

Simple solution (using int)

```
def addNumbersUsingSumFunction(numericStrings):
return str(sum(int(numString) for numString in numericStrings))
```

Adding Strings Solution (without using int())

```
import itertools
def addNumbers(numericStrings):
reversedNums = [list(number)[::-1] for number in numericStrings]
result = ''
carry = 0
for ind, digits in enumerate(itertools.zip_longest(*reversedNums, fillvalue='0')):
innerSum = carry
for digit in digits:
innerSum += ord(digit) - ord('0')
carry = innerSum // 10
result += str(innerSum % 10)
if carry:
result += str(carry)
return result[::-1]
```

Test Code:

```
print(addNumbersUsingSumFunction(['199', '1', '1'])) # 201
print(addNumbersUsingSumFunction(['999', '99', '9'])) #1107
print(addNumbersUsingSumFunction(['333', '333', '334'])) #1000
print(addNumbers(['199', '1', '1'])) # 201
print(addNumbers(['999', '99', '9'])) #1107
print(addNumbers(['333', '333', '334'])) #1000
```

```
string add(){
int mx = 0,c = 0;
string s[3]={"23456","21289124","1490280812"},t;
for(int i=0;i<3;i++)
if(mx < s[i].size())
mx = s[i].size();
for(int i=0;i<3;i++)
while(s[i].size()<mx)
s[i]="0"+s[i];
for(int i=mx-1;i>=0;i--){
int k = 0;
for(int j=0;j<3;j++)
k+=(s[j][i]-'0');
k += c;
t = to_string(k%10) + t;
c = k/10;
}
return t;
```

```
int mx = 0,c = 0;
string s[3]={"23456","21289124","1490280812"},t;
for(int i=0;i<3;i++)
if(mx < s[i].size())
mx = s[i].size();
for(int i=0;i<3;i++)
while(s[i].size()<mx)
s[i]="0"+s[i];
for(int i=mx-1;i>=0;i--){
int k = 0;
for(int j=0;j<3;j++)
k+=(s[j][i]-'0');
k += c;
t = to_string(k%10) + t;
c = k/10;
}
cout<<t;
```

Hint: use this method as a way of summing big numbers:

- denis.zayats March 06, 2018