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 Senior Software Engineer at Google:
Given an array of numbers, replace each number with the product of all the numbers in the array except the number itself *without* using division.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (7)
def mult(arr, num):
return reduce(lambda x,y: x*y if y!=num else x, arr)
arr = [mult(arr,i) for i in arr]
# O(n^2) time, O(n) space
Helpful Answer?
Yes |
No
Inappropriate?
4 of 4 people found this helpful
Now A[i] is simply B[i-1] * C[i+1].
Helpful Answer?
Yes |
No
Inappropriate?
lognums = [math.log10(n) for n in numbers]
sumlogs = sum(lognums)
return [math.pow(10, sumlogs-l) for l in lognums]
Helpful Answer?
Yes |
No
Inappropriate?
1 of 3 people found this helpful
tolal = A[0]*A[1]]*....*A[n-1]
now take a loop of array and update element i with A[i] = toal/A[i]
Since division is not allowed we have to simulate it.
If we say X*Y = Z, it means if X is added Y times it is equal to Z e.g.
2*3 = 6, which also means 2+2+2 = 6. This can be used in reverse to find how mach times X is added to get Z. Here is my C solution, which take pointer to array head A[0] and size of array as input
void ArrayMult(int *A, int size)
{
int total= 1;
for(int i=0; i< size; ++i)
total *= A[i];
for(int i=0; i< size; ++i)
{
int temp = total;
int cnt = 0;
while(temp)
{
temp -=A[i];
cnt++;
}
A[i] = cnt;
}
}
Speed in O(n) and space is O(1)
Helpful Answer?
Yes |
No
Inappropriate?
0 of 1 people found this helpful
#define NUM 10
int main()
{
int i, j = 0;
long int val = 1;
long A[NUM] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Store results in this so results do not interfere with multiplications
long prod[NUM];
while(j < NUM)
{
for(i = 0; i < NUM; i++)
{
if(j != i)
{
val *= A[i];
}
}
prod[j] = val;
i = 0;
val = 1;
j++;
}
for(i = 0; i < NUM; i++)
printf("prod[%d]=%d\n", i, prod[i]);
return 0;
}
Helpful Answer?
Yes |
No
Inappropriate?
int i;
int t1,t2;
t1 = array[0];
array[0] = prod(1, size, array );
for(i = 1; i < size; i++){
t2 = array[i];
array[i] = prod(i, array.size(), array)*t1;
t1 *= t2;
}
int prod(start, end, array){
int i;
int val(1);
for(i = start; i < end; i++ )
val *= array[i];
return val;
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
4 of 4 people found this helpful
by Jordan:
First, realize that saying the element should be the product of all other numbers is like saying it is the product of all the numbers to the left, times the product of all the numbers to the right. This is the main idea.
Call the original array A, with n elements. Index it with C notation, i.e. from A[0] to A[n - 1].
Create a new array B, also with n elements (can be uninitialized). Then, do this:
Accumulator = 1
For i = 0 to n - 2:
Accumulator *= A[i]
B[i + 1] = Accumulator
Accumulator = 1
For i = n - 1 down to 1:
Accumulator *= A[i]
B[i - 1] *= Accumulator
Replace A with B
It traverses A twice and executes 2n multiplicates, hence O(n) time
It creates an array B with the same size as A, hence O(n) temporary space