Google Interview Question
Software Engineer in TestsCountry: United States
public static int[] sum(int[] a, int[] b){
int len1 = a.length;
int len2 = b.length;
int len = (len1 > len2) ? len1: len2;
len = len+1;
int[] result = new int[len];
int i=len1-1;
int j=len2-1;
int k=0;
int carry = 0;
while(i >= 0 && j >= 0){
int sum = a[i] + b[j] + carry;
carry = sum / 10;
result[k++] = sum % 10;
i--;
j--;
}
while(i >= 0){
int sum = a[i] + carry;
carry = sum / 10;
sum = sum % 10;
result[k++] = sum;
i--;
}
while(j >= 0){
int sum = b[j] + carry;
carry = sum / 10;
sum = sum % 10;
result[k++] = sum;
j--;
}
if(carry > 0)
result[k++] = carry;
//reverse
int[] finalResult = new int[k];
i =0;
k--;
while(k>=0){
finalResult[i++] = result[k--];
}
return finalResult;
}
public class AddTwoArrays
{
public static int[] add(int[] a, int[] b){
int len1 = a.length;
int len2 = b.length;
int len = (len1 > len2)?len1:len2;
len = len + 1;
int [] result = new int[len];
int i = len1-1, j = len2-1, k = len -1, carry = 0, rest = 0;
while (i >= 0 && j >= 0){
rest = a[i] + b[j] + carry;
carry = 0;
while (rest >= 10){
rest = rest % 10;
carry = carry + 1;
}
result[k] = rest;
k--;
j--;
i--;
}
while (i >= 0){
rest = a[i] + carry;
carry = rest / 10;
rest = rest % 10;
result[k] = rest;
k--;
i--;
}
while (j >=0 ){
rest = b[i] + carry;
carry = rest / 10;
rest = rest % 10;
result[k] = rest;
k--;
j--;
}
result[0] = carry; // set the first digit of result as the carry
return result;
}
public static void main(String[] args)
{
int[] a = {3,9,9};
int[] b = {6};
int[] c = add(a,b);
if (c[0] > 0)
System.out.print(c[0]);
for (int i = 1; i < c.length; i++) {
System.out.print(c[i]);
}
}
}
std::vector<int> addTwoArrays(int* a, int aSize, int* b, int bSize)
{
int cSize = aSize > bSize? aSize+1: bSize+1;
std::vector<int> c(cSize);
int carry = 0;
for (int i = 1; i <= cSize; i++)
{
int sum = 0;
if(aSize-i >=0 && bSize-i >=0)
{
sum = a[aSize-i] + b[bSize-i];
}
else if (bSize-i >=0)
{
sum = b[bSize-i];
}
else if (aSize-i >= 0)
{
sum = a[aSize-i];
}
sum += carry;
carry = sum/10;
c[cSize-i] = sum%10;
}
return c;
}
std::vector<int> addTwoArrays(int* a, int aSize, int* b, int bSize)
{
int cSize = aSize > bSize? aSize+1: bSize+1;
std::vector<int> c(cSize);
int carry = 0;
for (int i = 1; i <= cSize; i++)
{
int sum = 0;
if(aSize-i >=0 && bSize-i >=0)
{
sum = a[aSize-i] + b[bSize-i];
}
else if (bSize-i >=0)
{
sum = b[bSize-i];
}
else if (aSize-i >= 0)
{
sum = a[aSize-i];
}
sum += carry;
carry = sum/10;
c[cSize-i] = sum%10;
}
return c;
}
private void add(int[] a, int[] b) {
int len = a.length > b.length ? a.length : b.length;
int[] result = new int[len];
int len1 = a.length;
int len2 = b.length;
int tmp1 = 1, tmp2 = 1;
for(int i: a){
tmp1 += i*((int)Math.pow(10,--len1));
}
for(int i:b){
tmp2 += i*((int)Math.pow(10,--len2));
}
tmp1--;
tmp2--;
tmp1 += tmp2;
//If need to convert back to integer array
String string = String.valueOf(tmp1);
for(int i=0; i<string.length(); i++){
result[i] = Character.getNumericValue(string.charAt(i));
}
}
Sorry, forgot to include the return result.
private int add(int[] a, int[] b) {
int len1 = a.length;
int len2 = b.length;
int tmp1 = 1, tmp2 = 1;
for(int i: a){
tmp1 += i*((int)Math.pow(10,--len1));
}
for(int i:b){
tmp2 += i*((int)Math.pow(10,--len2));
}
tmp1--;
tmp2--;
tmp1 += tmp2;
return tmp1;
}
//assumption array is given from lsb to msb
//eg add 1299 + 78 then array should be 9921 and 87 if not then it can be done by calling //reverse function
//m size of first array , n size of second array k = m>n? m:n +1
void (int a[],int m,int b[],int n,int res[],int k)
{
int carry,sum,i;
carry=0;
sum=0;
i=0;
while(i<k-1)
{
if(i<m)
sum=a[i]
if(i<n)
sum+=b[i]
sum+=carry;
carry=sum/10;
sum=sum%10;
res[i++]=sum;
}
if(carry)
res[i]=carry;
reverse(res); //to get the array in correct format
}
I do not fully understand the problem. Can you give an example please?
- GKalchev March 14, 2012