Google Interview Question for SDE1s


Team: maps
Country: United States




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

Launch two pointers Fwd and Bkw at the same time. Fwd is going in left-right/top-bottom direction and Bkw going in right-left/bottom-up direction. Compare values on every step and bail out if difference found. Terminate loop when Fwd == Bkw (i.e. they meet in the center).

- Anonymous June 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good answer. O(n*m) but we cannot do it faster without preprocessing.

- Anonymous June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it can be done faster by picking the number of iterations equal to the number of elements in the smaller dimension.

- Anant Anand Gupta June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shouldn't this be O(m+n) rather than O(m*n)???
You are just iterating through the whole matrix once in this approach...

- arun_lisieux June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous

In case of even sized matrix, (fwd==bkw) might not work.
I think It should be changed to (fwd < bkw)

- arun_lisieux June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//let a[][] be the matix

public bool IsSymmetric (int[][] a)
for(int i = 0;i < n;i ++)
{
for(int j = 0; j< n-i; j++)
{
if(a[i][j] != a[n-i][n-j]);
{
return false;
}
}
}
return true;

- PR June 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1) it can be rectangular - not always square
2) n not defined
3) the outer for loop should go only till the middle (I know the inner one won't execute but those are still precious cycles).

- Anonymous June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

slight modification of PR's code:
//let a[m][n] be the matix

public bool IsSymmetric (int[][] a)
for(int i = 0;i < m;i ++)
{
for(int j = 0; j< n; j++)
{
if(a[i][j] != a[m-i-1][n-j-1]);
{
return false;
}
}
}
return true;

- njnj June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

with a[n-i][n-j], array will be out of bound, you need [n-1-i][n-1-j]

- SunnyD June 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ifSameOnRotation(int a[R][C]){
    int i,j;
    for(i=0;i<=(R-1-i);i++){
                            for(j=0;j<C;j++)
                                            if(a[i][j]!=a[R-1-i][C-1-j])
                                                                        return 0;
    }
    return 1;
}

- Priyanka June 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Concatenate
row 0 with row n-1,
row 1 with row n-2,
row 2 with row n-3,
...
row n/2-1 with row n/2
You get a matrix of 2*m columns and n/2 rows.

Now each row should be a palindrome in order to fulfill the requirement.

Of course you don't really need to create a new matrix.
On each loop iteration deal with one concatenated row.

- Anon June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

180 rotation equal mean if you draw a line from bottom left to right up then both triangular half should be symetrical.then u traverse in the triangular array for both side nd figure out weather simmetrical or not

- go4gold June 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool is_the_same_when_rotated(bool a[], size_t rows, size_t cols)
{
	bool bRes = true;
	for (size_t i = 0; i <= (rows / 2) && bRes; i++)
	{
		for (size_t j = 0; j < cols; j++)
		{
			if (i == (rows / 2) && (j >= (cols / 2)))
			{
				break;
			}

			size_t currIdx = i * cols + j;
			if (a[currIdx] != a[((rows * cols) - 1) - currIdx])
			{
				bRes = false;
				break;
			}
		}
	}

	return bRes;
}

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

def is180RotatedEqual(matrix1, matrix2):
  if not matrix1:
    return not matrix2
  if len(matrix1) != len(matrix1[0]):
    return False

  for i in xrange(len(matrix1)):
    for j in xrange(len(matrix1)):
      if matrix1[i][j] != matrix2[len(matrix1) - i - 1][len(matrix1) - j - 1]:
        return False
  return True

print is180RotatedEqual(
    [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]],
    [[9, 8, 7],
     [6, 5, 4],
     [3, 2, 1]])
print is180RotatedEqual(
    [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]],
    [[9, 8, 7],
     [6, 5, 4],
     [9, 2, 1]])

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

We should first ask from interviewer regarding 180 degree of rotation on the basis of x axis or y axis. In both the condition we have to only check whether the row or column are palindrome or not. We can check it in O(ROW*COL) time complexity by traversing each row or column ( on the basis of x and y axis constraints ) from start and end simultaneously.

- anonymous June 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do we need to give a solution based on transformation matrix

- Pras July 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for i = 1 to MAX_ROWS
            while(MAX_ROWS - i + 1 > i)
                    for j = 1 to MAX_COLUMNS 
                                    while (MAX_COLUMNS -j + 1 > j)
                                           if Matrix(i,j) != Matrix(MAX_ROWS - i + 1,MAX_COLUMNS -j + 1) then 
                                                 message("Matrix not same when turned 180 deg")
                                                 break;
                                           endif
				endwhile
		endfor
	endwhile
endfor

- Madhu July 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

instead of break it should be exit.
Sorry for the pseudo code - haven't programmed since college.

- Madhu July 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def is180RotationSame(matrix):
	lastRowsIndex=len(matrix)-1
	lastColumnIndex=len(matrix[0])-1
	
	for i in range (lastRowsIndex+1):
		for j in range(lastColumnIndex+1):
			if (i<=lastRowsIndex-i or j<=lastColumnIndex-j):
				if(matrix[i][j]!=matrix[lastRowsIndex-i][lastColumnIndex-j]):
					return False
			else:
				#print "Breaking at %d row and %d column"%(i+1,j+1)
				return True

	
if __name__=='__main__':
	#matrix=[[i for i in range(5)] for j in range(3)]
	matrix=[[1,2,3,4],[2,6,5,4],[4,5,6,2],[4,3,2,1]]
	print matrix
	print is180RotationSame(matrix)

- Roopal Garg August 23, 2013 | 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