Microsoft Interview Question
InternsCountry: India
Interview Type: In-Person
Use NxN int matrix and 2N + 2 integers representing the sum of N cols, N rows and 2 diagonals.
Player 1's choices will be marked as 1.
Player 2's choices will be marked as -1.
Add that mark (+-1) to the relevant row/col/diag counters.
Player 1 wins if after adding the mark the row/col/diag is sum is N.
Player 1 wins if after adding the mark the row/col/diag is sum is -N.
They draw if matrix is full.
Calculating game result is immediate.
For every row/column, maintain a field called SUM. When player 1 marks, add 2 to the sum, when player2 marks, add 5 to the sum. If the sum ever reaches 2*N, then player 1 win, If the sum reachers 5*N, then player 2 wins. We only need to check/update the sum for the row/column where the last player has played.
Note: instead of 2,5 we can use any two numbers which are relatively prime.
Have an int double array (n x n)
- Neerad October 24, 2012For every row and column, have three extra fields
1. Corrupted (If this row or column is filled by both players atleast once)
2. First move (Who filled the row/column first - 1 or 2)
3. No.of blocks filled (Count)
All are initialized to zero. You have player1 and player2
When a player makes an entry, you have to check those 3 variables in corresponding row and column. Follow the following steps:
1. If corrupted is true, you need not worry
2. If the person who made the move now != first person, corrupted = true and jump off
3. No.of blocks++ , If no.of blocks == N, this player has won. End of story :D