Google Interview Question
1,223 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Intern at Google:
If integer array used to store big integers (one integer store one digit), implement arithmetic operations.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (5)
def addBigNumbers(n1, n2):
if not n1 or not n2:
return n1 + n2
x = n1.pop() + n2.pop()
if x >= 10:
return addBigNumbers(addBigNumbers(n1, [1]), n2) + [x % 10]
else:
return addBigNumbers(n1, n2) + [x]
# Or If using ['1', '2', '3'] style
def addBigNumbersChar(n1, n2):
if not n1 or not n2:
return n1 + n2
x = ord(n1.pop()) + ord(n2.pop()) - 2 * ord('0')
if x >= 10:
return addBigNumbers(addBigNumbers(n1, [1]), n2) + [x % 10]
else:
return addBigNumbers(n1, n2) + [x]
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
package com.google.testing;
public class BigIntegerOperations {
/**
* @param args
*/
public static void main(String[] args) {
int[] number1 = {4,5,7,0};
int[] number2 = {5,0,7,0};
System.out.println(add(number1,number2));
}
private static int add(int[] number1, int[] number2) {
int num1 = getNumber(number1);
int num2 = getNumber(number2);
return num1+num2;
}
private static int getNumber(int[] arrayNumber)
{
int number = arrayNumber[arrayNumber.length-1];
for(int i=arrayNumber.length-2,j=1;i>=0;i--,j++)
number+=(arrayNumber[i]*(getMultiple(j)));
return number;
}
private static int getMultiple(int times)
{
int number = 1;
for(int i=0;i<times;i++)
number*=10;
return number;
}
}
Helpful Answer?
Yes |
No
Inappropriate?
People dont read what other guy wrote and how there solution compare.
rediculous.
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
0 of 0 people found this helpful
by Interview Candidate: