Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer In Test at Google:
How would you determine if someone has won a game of tic-tac-toe on a board of any size?
| Tags: | brain teaser See more , See less 8 |
See more for this Google Software Engineer In Test Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (11)
0 of 3 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
3 of 3 people found this helpful
I think they must mean how can you determine if someone has won during a game in real time, as in checking after every move. This can be solved with a strategy in constant time.
My solution would be: Create an array of size 2n+2 at the beginning of the game and fill it with zeros. Each spot in the array will be a sum of X's or O's horizontally (the first n places in the array), vertically (the second n places in the array) and diagonally (the last 2 places). Then with every move, you add 1 to the 2 places (or 3 if on a diagnol) of the array if X, and subtract 1 if its an O. After adding you check and see if the value of the array is equal to n or -n, if it is, n mean X has won and -n means O has won.
I would bet there is a more elegant solution than creating a large array, but since this isn't my job interview I can't be bothered trying to figure one out. :)
Helpful Answer?
Yes |
No
Inappropriate?
1 of 7 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 7 people found this helpful
While this may ignore the "engineering" side of the question, it does demonstrate the logic of searching for the simplest answer becuase that solution will be the same regardless of the board configuration.
Helpful Answer?
Yes |
No
Inappropriate?
1 of 4 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
Assume that, for our problem, we are only checking if the winner is 'X'. We have to make at least one pass through the game board, but we should be able to solve the problem in one pass without checking any cell twice. Target running time O(N^2) for a board of size NxN.
boolean checkXWinner(int[][] a, int n){
int[] diagonalSums = new int[2];
int[] columnSums = new int[n];
initialize diagonalSums and columnSums with zeroes;
int rowSum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j] = 'X')
rowSum++;
columnSums[j]++;
if (i == n-1 && columnSums[j] == n) return true
else if (i == j)
diagonalSums[0]++;
if (i == n-1 && diagonalSums[0] == n) return true
else if (i = n-1-j)
diagonalSums[1]++;
if (j == 0 && diagonalSums[i] == n) return true
if (rowSum == n) return true
Helpful Answer?
Yes |
No
Inappropriate?
else rowSum = 0 // Need to reset the row counter
Helpful Answer?
Yes |
No
Inappropriate?
0 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
1 of 3 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 5 people found this helpful
by B: