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.
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");
}

- arnuld February 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 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];

- Ritesh February 08, 2018 | Flag Reply
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;
		}
	}
}

- Interviewer February 07, 2018 | Flag Reply
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();
    }

- Dmitry Gorbunov February 10, 2018 | Flag Reply
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();
    }

- Anonymous February 10, 2018 | Flag Reply
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)

- Anonymous February 10, 2018 | Flag Reply
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;
}

- shivam kumar singh February 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 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;
}

- Pavel February 15, 2018 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More