Google Interview Question for Software Engineer / Developers






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

Java solution

void findSumOfColumns(int[] array, int nRows) {
	if (array.lenth < nRows)
	{
		System.out.println("Error: msg");
		return;
	}
	int nCols = Math.ceil((double)array.length/nRows);
	int[] sums = new int[nCols];
	//for loop to initialize sums to zero
	int elems = array.length;	
	for (int i=0; i<elems; i++) {
		sums[i%nCols] += array[i];
	}
	for (int i=0; i<nCols; i++)
		System.out.println(sums[i]);

}

- Abhiraj December 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if the array is of size 9 and rows == 4. the result will look like it has 3 rows only

- al April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int* findArraySum(int* arr, int num_elem, int num_rows) {
int rem;
if((rem = num_elem%num_rows) != 0)
return null;
int* result = (int*)malloc(rem*sizeof(int));
memset(result, 0, rem);
for(int i = 0; i < rem; i++) {
for(int j = 0; j < num_rows; j++) {
result[i] = result[i] + arr[i+rem*j];
}
}
return result;
}

This should run in O(n) where n is the number of elements in the given array.

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

int* findArraySum(int* arr, int num_elem, int num_rows) {
  int rem;
  if((rem = num_elem%num_rows) != 0)
    return null;
  int* result = (int*)malloc(rem*sizeof(int));
  memset(result, 0, rem);
  for(int i = 0; i < rem; i++) {
    for(int j = 0; j < num_rows; j++) {
      result[i] = result[i] + arr[i+rem*j];
    }
  }
  return result;
}

This should run in O(n) where n is the number of elements in the given array.

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

which google is it????

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

I didnt get the ques please explain a bit more

- Aditya June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void sum1darray(int a[], int cols, int rows, int*& result)
{
    result = new int[cols];
    for(int i = 0; i < cols; i++)
    {
        int sum = 0;
        for(int j = 0; j < rows; j++)
            sum+=a[j*cols + i];
        result[i] = sum;
    }
}

- ssss June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey22451" class="run-this">#include <stdio.h>
#define MAX_COLS 10

int main()
{
int total_nums, i, k, num_rows, num_cols;
int A[8]= {3,4,7,2,2,6,0,9};
int sum[MAX_COLS];

//Initialize
for (i = 0; i < 10; i++)
{
sum[i] = 0;
}
i = 0;
k = 0;

scanf("%d", &num_rows);
// num_rows = 4;
total_nums = 8;
num_cols = (total_nums / num_rows) + ((total_nums % num_rows) ? 1: 0);

for (; i < total_nums; i++)
{
k = i % num_cols;
sum[k] += A[i];
}
for (i = 0; i < num_cols ; i++)
{
printf("The sum of %d column is %d\n",i, sum[i]);
}
}


</pre><pre title="CodeMonkey22451" input="yes">
</pre>

- Anonymous June 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) solution:

public static int[] sumOfCol(int[] array, int nrows) {
		if (array.length % nrows == 0) {
			int ncols = array.length / nrows;
			int[] cols = new int[ncols];
			
			int f = 0;
			for (int i = 0 ; i < array.length ; i++) {
				cols[f] += array[i];
				f++;
				if (f >= ncols) {
					f = 0;
				}
			}
			
			return cols;
		}
		
		return new int[0];
	}

- TheWiz June 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Think about if you have 9 elements in the array, and you want the row number to be 4, it's not possible.

- shoushou July 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So obviously numRows must divide the number of elements and a good implementation should check for that.

- Bullocks July 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int i=0,j=0,k=0,sum=0;
int r=3;//row
int a[]={3,4,7,2,2,6,0,9,5,8,11};
int n=sizeof(a)/sizeof(int);
j=n/r;//column

while(k<j)
{
for(i=k;i<n;i+=j)
{
sum=sum+a[i];
}
cout<<sum<<endl;
sum=0;
k++;
}

return 0;
}

- software engineer July 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] arr= {3,4,7,2,2,6,0,9};


int row=4;
int col=arr.length/row;

for(int k=0;k<col;k++){
int sum=0;
for(int i=0;i<row;i++){
sum+=arr[col*i+k];
}
System.out.println("sum : " + k + ": " + sum);
}

- sundi133 January 29, 2012 | 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