Google Interview Question
Site Reliability EngineersTeam: Site reliabilty
Country: United States
Interview Type: Phone Interview
int* add( int *src, int len, int data)
{
int *res;
int i;
for ( i = len - 1; i > = 0; i-- )
{
if ( data == 0)
{
break;
}
data = (src[i] + data) / 10;
src[i] = (src[i] + data) % 10;
}
if ( data == 0 )
return src;
else
{
res = (int*)malloc(sizeof(int)*(len+1));
memset(res, 0, len+1);
res[0] = data;
memcpy(res[1], src, len);
free(src);
return res;
}
}
Corrections are appreciated :)
just simple string addition
#include<stdio.h>
#include<string.h>
int main()
{
int k1,k2,i,j=0,temp1,temp2,temp=0;
char str1[10000];
char str2[10000];
char stradd[100000];
scanf("%s",str1);
scanf("%s",str2);
k1=strlen(str1);
k2=strlen(str2);
k1=k1-1;
k2=k2-1;
temp=0;
for(i=k1;i>=0;i--)
{
if(k2>=0){
temp1=(str1[i]-'0')+(str2[k2]-'0')+temp;
//printf("%d\n",temp1);
temp2=temp1%10;
stradd[j]=temp2+'0';
// printf("%c\n",stradd[j]);
j=j+1;
k2=k2-1;
temp=temp1/10;
if(i==0&&temp!=0)
printf("%d",temp);
}
else
{
temp1=(str1[i]-'0')+temp;
// printf("%d\n",temp1);
temp2=temp1%10;
stradd[j]=temp2+'0';
//printf("%c\n",stradd[j]);
temp=temp1/10;
if(i==0&&temp!=0)
printf("%d",temp);
j=j+1;
}
}
for(i=j-1;i>=0;i--)
{
printf("%c",stradd[i]);
}
return 0;
}
with this code we can calculate additon upto 1000000 digits :)
{{
# add 1 to last number in array
# if number is 9, change it to zero, and increment the next number by one.
# bcase case return number otherwise.
#
def add_one(array, curr_index)
array[curr_index]
if array[curr_index] != 9
array[curr_index] += 1
return array
else # it is 9
array[curr_index] = 0
if curr_index.zero?
array.unshift(1)
return array
end
add_one(array, curr_index - 1)
end
end
a = [1,0,0,0]
p add_one(a, a.length - 1)
a = [1,0,0,9]
p add_one(a, a.length - 1)
a = [1,9,9,9]
p add_one(a, a.length - 1)
a = [9,9,9,9]
p add_one(a, a.length - 1)
}}
Here's a small version in Python:
def convert(a):
for x in range(len(a) -1, -1, -1):
a[x] = (a[x] + 1) % 10 ## If the digit gets to 10, the modulo operation will make it 0
if a[x]: ## If it isn't 0, there's no more carry and we're done
return a
return [1] + a ## If we got to this point, we'll have to add another digit
my recursive answer in Java:
public class Add {
public static void main(String[] args) {
int[] array = {9,9,9,9};
int[] res = add(array, 1, array.length-1);
for (int i = 0; i < res.length; i++) {
System.out.print(res[i]);
}
System.out.println();
}
public static int[] add(int[] array, int num, int idx) {
if(idx < 0) {
int[] newArray = new int[array.length+1];
for (int i = 0; i < array.length; i++) {
newArray[i+1] = array[i];
}
array = newArray;
array[0] = num;
// allocate a new array
return array;
}
// recursion move:
int temp = array[idx] + num;
// we want to attempt and add num to the current cell. we consider whether this
// will affect the next digit to the right
if(temp / 10 == 0) { // temp is a single digit
array[idx] += num;
} else {
// temp is a 2-digits number
// add to array[idx] the less significant digit
array[idx] = temp % 10;
// call recursively to the next digit
array = add(array, temp/10, idx-1);
}
return array;
}
}
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[4]={0,0,1,9},i;
int size,length;
int carry=1;
size=sizeof(a)/sizeof(a[0]);
for(i=size-1;i>=0;i--){
if(a[i]==9 && (carry==1)){
a[i]=0;
carry=1;
}
else if (carry==1){
a[i]=a[i]+1;
carry=0;
}
}
printf("\n\n");
for(i=0;i<size;i++)
printf("%d ",a[i]);
printf("\n\n");
return 0;
}
What's the point of this question?
int *foo2(int a[], int n)
{
int *b, carry = 1, t;
if ((b = (int *)malloc(n + 1)) == NULL) {
fprintf(stderr, "malloc failed\n");
return NULL;
}
for (int i = n - 1; i >= 0; i--) {
t = a[i] + carry;
if (t == 10) {
t = 0;
carry = 1;
} else {
carry = 0;
}
b[i + 1] = t;
}
b[0] = carry;
return b;
}
int main()
{
int a[] = { 2, 0, 9, 9 }, *b;
b = foo(a, 4);
if (b != NULL) {
for (int i = 0; i < 5; i++) printf("%d ", b[i]);
printf("\n");
}
free(b);
b = foo2(a, 4);
if (b != NULL) {
for (int i = 0; i < 5; i++) printf("%d ", b[i]);
printf("\n");
}
free(b);
return 0;
}
With bits of Java library classes (Math, Arrays):
import java.util.Arrays;
public class NumberAsArray {
public static void main(String[] args) {
System.out.println(Arrays.toString(inc(new int[] {1,0,0,1})));
System.out.println(Arrays.toString(inc(new int[] {3,7,9,9})));
System.out.println(Arrays.toString(inc(new int[] {9,9,9,9,9})));
}
private static int[] inc (int[] is) {
// Convert array to number
int a = 0;
for (int i = 0; i < is.length; i++) {
a += is[i] * Math.pow(10, is.length - i - 1);
}
// Increment number
a++;
// Convert number to array
int[] result = new int[(int) Math.log10(a) + 1];
for (int i = 0; i < result.length; i++) {
result[result.length - i - 1] = a % 10;
a = a / 10;
}
return result;
}
}
Start from the last index of the array and add 1... if that sum is == 10, make that number = 0, and carry "1" to the preceding num... exactly like you would add a number..
it becomes tricky when the number is such that adding 1 increases the number of elements by 1... for example, the number is 999... then adding 1 will make it 1000 which is 1 digit more than that of the given number...
in that case, you could check the value of i after the while loop...
- JustCoding September 27, 2012