Google Interview Question
1,223 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
How would you determine if someone has won a game of tic-tac-toe on a board of any size?
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
public class TicTacToe {
public static boolean isWinner(int[][] arr, int player){
int count = 0;
for (int i=0; i<arr.length; ++i){
for (int j=0; j<arr.length; ++j){
if (arr[i][j] == player){
++count;
}
else{
break;
}
}
}
if (count == arr.length){
return true;
}
count = 0;
for (int j=0; j<arr.length; ++j){
for (int i=0; i<arr.length; ++i){
if (arr[i][j] == player){
++count;
}
else{
break;
}
}
}
if (count == arr.length){
return true;
}
count = 0;
for (int i=0; i<arr.length; ++i){
for (int j=i; j==i; ++j ){
if (arr[i][j] == player){
++count;
}
else{
break;
}
}
}
if (count == arr.length){
return true;
}
count = 0;
for (int j=0; j<arr.length; ++j){
for (int i=j; i==j; ++i ){
if (arr[i][j] == player){
++count;
}
else{
break;
}
}
}
if (count == arr.length){
return true;
}
return false;
}
public static void main(String[] args) {
int[][] arr = new int[2][2];
arr[0][0] = 1;
arr[1][1] = 1;
System.out.println(isWinner(arr, 1));
}
}
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
1 of 9 people found this helpful
by reed: