## Google Interview Question for Senior Software Development Engineers

Team: TEZ
Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
2
of 2 vote

Algorithm : Take the product of all item in the Array, loop from 0 to array.length make an array having item as- allProduct/array[i];

Comment hidden because of low score. Click to expand.
2
of 2 vote

public int[] product(int[] A, int[] B) {
int mul = 1;
int zeros = 0;
for (int i=0; i<A.length; i++) {
if (A[i] != 0) mul *= A[i];
if (A[i]==0) zeros++;
}
for (int i=0; i<A.length; i++) {
if (zeros>1) B[i] = 0;
else if (A[i] == 0) B[i] = mul;
else B[i] = mul/A[i];
}
return B;
}

Comment hidden because of low score. Click to expand.
1
of 1 vote

``````#include <stdio.h>

enum { SIZE = 3 };

void printArr(int ai[]);

int main(void)
{
int arrA[] = {1,2,3,4};
int arrB[SIZE+1];
int m = 1;

printArr(arrA);
for(int i = 0; i <= SIZE; ++i)
{
for(int j = SIZE; j >= 0; --j)
{
if(i != j)
{
m = m * (arrA[j]);
}
}
arrB[i] = m;
m = 1;
}

printArr(arrB);

return 0;
}

void printArr(int ai[])
{
for(int i = 0; i <= SIZE; ++i)
printf("%d ", ai[i]);

printf("\n\n");
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````*/
public class ProductOfAllExceptSelf
{
public double[] getProducts(int[] input) throws Exception
{
if(input == null || input.length < 2)
throw new Exception("Invalid array - at least 2 elements needed");
else if(input.length == 2)
{
return new double[]{input[1], input[0]};
}
else
{
// check if zero exists
int N = input.length;
double[] result = new double[N];
int i, zeroCount = 0, zeroIndex = -1;
for(i = 0; i < N; i++)
{
if(input[i] == 0)
{
++zeroCount;
zeroIndex = i;
}
}

switch(zeroCount)
{
case 0:
// no zeros
double[] CP = new double[N];
CP[0] = input[0];

for(i = 1; i < N; i++)
{
CP[i] = CP[i - 1] * input[i];
}

for(i = 0; i < N; i++)
{
if(i == 0)
result[i] = CP[N - 1] / CP[0];
else if(i == N - 1)
result[i] = CP[N - 2];
else
result[i] = (CP[N - 1]*CP[i - 1])/CP[i];
}

break;

case 1:
// exactly one element is zero
int product = 1;
for(i = 0; i < N; i++)
{
if(i != zeroIndex)
{
product *= input[i];
}
else
{
result[i] = 0;
}
}

result[zeroIndex] = product;
break;

default:
// 2 or more zeros
for(i = 0; i < N; i++)
{
result[i] = 0;
}

break;
}

return result;
}
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static int[] productOfAllElementsExceptNth(int[] a) {
return IntStream.range(0, a.length)
.map(i -> IntStream.range(0, a.length)
.filter(j -> j!=i)
.map(j -> a[j])
.reduce(1, (x, y) -> x * y))
.toArray();
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static int[] productOfAllElementsExceptNth(int[] a) {
return IntStream.range(0, a.length)
.map(i -> IntStream.range(0, a.length)
.filter(j -> j!=i)
.map(j -> a[j])
.reduce(1, (x, y) -> x * y))
.toArray();
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

import numpy as np

``````A = np.arange(1,4)
C = np.zeros(len(A))

for i in range(len(A)):
C[i] = np.product([A[c] for c in range(len(A)) if c!=i])

print(A)
print(C)``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public int[] function(int[] A, int[] B){
int mul = 1;
for(int i=0;i<A.length;i++){
mul *= A[i];
}
for(int i=0;i<A.length;i++){
B[i] = mul/A[i];
}
return B;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````long[] function(int[] a) {
if (a.length < 2) return a

long[] res = new int[a.length]
long m = 1
for (int e in a) {
m *= e
}

for (int i; i < a.length; i++) {
res[i] = m / a[i]
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````BigInteger[] function(int[] a) {
if (a.length < 2) return a

BigInteger[] res = new BigInteger[a.length]
BigInteger m = 1
for (int e in a) {
m *= e
}

for (int i; i < a.length; i++) {
res[i] = m / a[i]
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````from operator import mul
def buildB(arr):
product = reduce(mul, arr)
return [product/num for num in arr]``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static long[] productExceptItself (int[] arr) {
if (arr == null || arr.length <= 1) return new long[0];
int len = arr.length;
for (int i = 1; i < len; i++) {
}
long rightProd = 1L;
for (int i = len-1; i >= 0; i--) {
rightProd *= arr[i];
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````const productArray = arr => {
let result = [];
let product = arr.reduce((prod, item) => prod * item , 1);
arr.forEach(el => {
result.push(product/el);
});

return result;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````const productArray = arr => {
let result = [];
let product = arr.reduce((prod, item) => prod * item , 1);
arr.forEach(el => {
result.push(product/el);
});

return result;
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.