Google Interview Question for Software Engineer in Tests


Country: United States




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

I do not fully understand the problem. Can you give an example please?

- GKalchev March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] sum(int[] a, int[] b){
int len1 = a.length;
int len2 = b.length;

int len = (len1 > len2) ? len1: len2;
len = len+1;
int[] result = new int[len];

int i=len1-1;
int j=len2-1;
int k=0;
int carry = 0;
while(i >= 0 && j >= 0){
int sum = a[i] + b[j] + carry;
carry = sum / 10;
result[k++] = sum % 10;
i--;
j--;
}

while(i >= 0){
int sum = a[i] + carry;
carry = sum / 10;
sum = sum % 10;
result[k++] = sum;
i--;
}
while(j >= 0){
int sum = b[j] + carry;
carry = sum / 10;
sum = sum % 10;
result[k++] = sum;
j--;
}

if(carry > 0)
result[k++] = carry;

//reverse
int[] finalResult = new int[k];
i =0;
k--;
while(k>=0){
finalResult[i++] = result[k--];
}
return finalResult;
}

- swapsmagic April 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AddTwoArrays
{
public static int[] add(int[] a, int[] b){
int len1 = a.length;
int len2 = b.length;

int len = (len1 > len2)?len1:len2;
len = len + 1;
int [] result = new int[len];

int i = len1-1, j = len2-1, k = len -1, carry = 0, rest = 0;
while (i >= 0 && j >= 0){
rest = a[i] + b[j] + carry;
carry = 0;
while (rest >= 10){
rest = rest % 10;
carry = carry + 1;
}
result[k] = rest;
k--;
j--;
i--;
}

while (i >= 0){
rest = a[i] + carry;
carry = rest / 10;
rest = rest % 10;
result[k] = rest;
k--;
i--;
}

while (j >=0 ){
rest = b[i] + carry;
carry = rest / 10;
rest = rest % 10;
result[k] = rest;
k--;
j--;
}

result[0] = carry; // set the first digit of result as the carry

return result;
}

public static void main(String[] args)
{
int[] a = {3,9,9};
int[] b = {6};

int[] c = add(a,b);

if (c[0] > 0)
System.out.print(c[0]);
for (int i = 1; i < c.length; i++) {
System.out.print(c[i]);
}
}
}

- eboyGTK April 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::vector<int> addTwoArrays(int* a, int aSize, int* b, int bSize)
{
	int cSize = aSize > bSize? aSize+1: bSize+1;

	std::vector<int> c(cSize);

	int carry = 0;

	for (int i = 1; i <= cSize; i++)
	{
		int sum = 0;
		if(aSize-i >=0 && bSize-i >=0)
		{
			sum = a[aSize-i] + b[bSize-i];
		}
		else if (bSize-i >=0)
 		{
			sum = b[bSize-i];
		}
		else if (aSize-i >= 0)
		{
			sum = a[aSize-i];
		}
		sum += carry;
		carry = sum/10;
		c[cSize-i] = sum%10;
	}

	return c;
}

- ns July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::vector<int> addTwoArrays(int* a, int aSize, int* b, int bSize)
{
	int cSize = aSize > bSize? aSize+1: bSize+1;

	std::vector<int> c(cSize);

	int carry = 0;

	for (int i = 1; i <= cSize; i++)
	{
		int sum = 0;
		if(aSize-i >=0 && bSize-i >=0)
		{
			sum = a[aSize-i] + b[bSize-i];
		}
		else if (bSize-i >=0)
 		{
			sum = b[bSize-i];
		}
		else if (aSize-i >= 0)
		{
			sum = a[aSize-i];
		}
		sum += carry;
		carry = sum/10;
		c[cSize-i] = sum%10;
	}

	return c;
}

- ns July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int add(char[] a, const int a_len, char[] b, const int b_len)
{
 int sum = 0;
 int mul = 1;
 for (int i = a_len-1, j = b_len-1; i>=0 || j>=0; )
 {
  sum += ((i<0? 0 : a[i--]-’0’) + (j<0 ? 0 : b[j--]-’0’)) * mul;
  mul *= 10;
 }

 return sum;
}

- Anonymous October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void add(int[] a, int[] b) {
		int len = a.length > b.length ? a.length : b.length;
		int[] result = new int[len];
		int len1 = a.length;
		int len2 = b.length;
		
		int tmp1 = 1, tmp2 = 1;
		for(int i: a){
			tmp1 += i*((int)Math.pow(10,--len1));
		}
		for(int i:b){
			tmp2 += i*((int)Math.pow(10,--len2));
		}
		tmp1--;
		tmp2--;
		tmp1 += tmp2;
		
		//If need to convert back to integer array
		String string = String.valueOf(tmp1);
		for(int i=0; i<string.length(); i++){
			result[i] = Character.getNumericValue(string.charAt(i));
		}
		
	}

- brucyy December 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, forgot to include the return result.

private int add(int[] a, int[] b) {
		int len1 = a.length;
		int len2 = b.length;
		
		int tmp1 = 1, tmp2 = 1;
		for(int i: a){
			tmp1 += i*((int)Math.pow(10,--len1));
		}
		for(int i:b){
			tmp2 += i*((int)Math.pow(10,--len2));
		}
		tmp1--;
		tmp2--;
		tmp1 += tmp2;

	return tmp1;
}

- brucyy December 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

//assumption array is given from lsb to msb
//eg add 1299 + 78 then array should be 9921 and 87 if not then it can be done by calling //reverse function
//m size of first array , n size of second array k = m>n? m:n +1
void (int a[],int m,int b[],int n,int res[],int k)
{
int carry,sum,i;
carry=0;
sum=0;
i=0;
while(i<k-1)
{
if(i<m)
sum=a[i]
if(i<n)
sum+=b[i]

sum+=carry;

carry=sum/10;
sum=sum%10;
res[i++]=sum;
}


if(carry)
res[i]=carry;

reverse(res); //to get the array in correct format
}

- Anonymous March 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

u didn't seems to add the previous carry to the next element in res, instead you seems overwrote the next res element with the a[i] + b[i]

- tank March 29, 2012 | Flag


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