Google Interview Question for Software Engineer / Developers






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

My solution:

{{

#define SIZE 3
#define MSIZE -3

int row[SIZE];
int col[SIZE];

int init () {
int i, j;
for (i=0; i<SIZE; ++i)
row[i] = 0;
for (j=0; j<SIZE; ++j)
row[j] = 0;
}

// 0: no win
// 1: 1st player win
// 2: 2nd player win
int check (int x, int y, int p){ // O(1)
if (p == 1){
row[y]++;
col[x]++;
}
else {
col[x]--;
row[y]--;
}

if (row[y] == SIZE ||
row[y] == MSIZE ||
col[x] == SIZE ||
col[x] == MSIZE)
return p; //

return 0;
}

}}

- Peng Du February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the "win criteria"? is it having 3 of a kind in a row or all the same in a row. If it's the former than this wouldn't work for grids bigger than 3*3.

- volongoto February 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

brilliant solution peng. it can be easily extended by increasing SIZE constant.

- pm April 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

kewl soln !

- kewl June 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution. You are however not checking for diagonal conditions though :)

- IntwPrep October 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

Great Solution. However diagonals need to be handled as IntwPrep pointed out.

if (p == 1){ 
row[y]++; 
col[x]++; 

if (x == y)
   diag ++ // similarly decrement for the other player.

if (y == n - x )
   antidiag ++ // similarly decrement for the other player.

}

// finally check 

diag == SIZE or diag ==  MSIZE 
or antidiag == SIZE or antidiag == MSIZE

- sppl March 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also we can have another counter for number of moves made.
moves=0;
moves++ for every move, If moves = n*n , then game is over

- iwanna July 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is to check if a player wins as the game progresses. I understand that you are given a snapshot of the grid at any given time and you need to determine who won.

- cool.fire.ra July 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works only in case the SIZE of the game determines how many X's or O's you need. 3x3 -> you need to have 3 same symbols. I understood it as an NxN game where you need to have 5 same symbols.

- mbriskar May 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How can it be done in O(n). If we are checking the contents of Row(n) wont it take O(n) time.
I think it will take O(n) + O(n) +O(n) + O(n)
Check through the row n, check through the column n, check through the left diagonal and the right diagonal

- abhimanipal February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i guess the interviewer was asking the author to check the winning condition in every step during the game

- fox February 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even then .... For example its 3*3 tic tac toe game and I fill the square 1*1, the program has to check the square
1*1, 1*2, 1*3
1*1, 2*1, 3*1
1*1, 2*2, 3*3

I dont see how this can be done in O(1)

- abhimanipal February 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One possinel solution is keep one more row and column and one extra int var. These will store sum of corresponding row column and diagonals. After each move these will be updated. I see only this is the way to solve it in O(1)

- LA February 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it's O(1) space complex. but I don't think it O(1) time comlex........

- beyondfalcon April 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can do it in o(1) time. There are possible 6 winning conditions i.e H1, h2, h3, V1, V2, V3, D1, D2 where H stands for horizontal, v for verticle and d for diagonal and number stands for its level.

Now when we have any entry increment the value in the concerned level. e.g if user enters something in center than each condition will be incremented. Hence with this the one which got value of 3 will be the answer.

- Sarbjit Singh February 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

haha.. is this O(1) coz u wrote it in words?

- Mahesh April 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

add above for diagonal elements also
diag[3]={0};
if(p==1){
if(x==y)
diag[x]++;
}
else
diag[x]--

- Anonymous February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There should be two diagonals for each location in the grid and both should be checked. On the other hand, as stated in my previous comment, this solution assumes the win criteria is having all items in a row/ column/ diagonal to be the same. Is it the "win criteria" of tic-tac-toe?

- volongoto February 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this can simply be done in O(1) with the use of a 2X4 array (set of 4 for each player, where each variable keeps count of middle row,middle col,middle diagonal,middle back-diagonal).For each move check update the associated variable. In order to check for winning condition just check whether any variable is equal to N.
Below is the code:

int count[2][4],N;
//assuming the zero based indexing...
void update(int type, int row, int col)
{
if(row == col)++count[type][0]; //diagonal;
if(row == N/2)++count[type][1]; //row;
if(col == N/2)++count[type][2]; //column;
if(row+col == N-1)++count[type][3]; //back diagonal
}

bool win(int type)
{
for(int i=0;i<4;++i)if(arr[type][i] == N)return true;
return false;
}

int main()
{
cin >> N;
int moves = 0;
while(1)
{
int r = getPlayer_row();
int c = getPlayer_col();
int type = getPlayer_type();//0 or 1 corresponding to'X' //or 'O'
update(type,r,c);
moves++;
if(moves >= N*N || win(0) || win(1))break;
}
}

- brainF**K February 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

volongoto is right, we need to also track the two diagonals. but only a single variable is enough for each diagonal:

diag=0;
diag_back=0;
if(p==1){
if(x==y)
diag++;
else if (x+y==SIZE) // back-diagonal
diag_back++;
}
else {
if(x==y)
diag--;
else if (x+y==SIZE) // back-diagonal
diag_back--;
}

if (diag == SIZE || diag == MSIZE
|| diag_back == SIZE || diag_back == MSIZE)
return p;
else {
// check rows and columns
}

return 0;

- peng April 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

volongoto is right, we need to also track the two diagonals. but only a single variable is enough for each diagonal:

diag=0;
diag_back=0;
if(p==1){
  if(x==y)
    diag++;
  else if (x+y==SIZE) // back-diagonal
    diag_back++;
}
else {
  if(x==y)
    diag--;
  else if (x+y==SIZE) // back-diagonal
    diag_back--;
}

if (diag == SIZE || diag == MSIZE 
   || diag_back == SIZE || diag_back == MSIZE)
  return p;
else {
  // check rows and columns
}

return 0;

- peng April 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If someone enters a 1 in (m,n), add 1 to row[m], col[n]; also if m==n add to diagonal[m], and if m==(N-n), add to antidiag[m]. If 0 is entered, add -1 instead. If at any point of time, the updated row/col/diag/antidiag entry becomes either N or -N, you have a winner; if N, then you have 1 winnig; if -N then you have 0 winning. Since it invovles only a constant number of updates at every turn, it is O(1) checking for victory (at every turn).

- HazTehCode April 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is there a solution in python?

- Anonymous October 04, 2015 | 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