Google Interview Question
Software EngineersCountry: India
Interview Type: In-Person
A simple AI could check whether the number of valid positions before and after a move and prefer moves which create an odd or even number of moves depending on which one would allow the AI to have the final turn. This would require first writing a method to check the valid moves on the board with a given state. I'd talk about how you could train an AI but obviously that wouldn't be implemented in an interview.
If the board has a cell symmetry in it then the player who can first exploit it will win. Example on a even*even board the second player can always win, by simply imitating all moves made by the first player ensuring that his moves are symmetrical against the same diagonal. This way the number of domino positions is even, and they are consumed at identical rates across the two diagonally opposed triangles.
On the other hand if the board is even * odd, then the first player has winning strategy by occupying the center cells. Once done all remaining cells are symmetric by a diagonal, and the first player merely has to repeat the same move, thus again ensuring that the free positions are consumed at an identical rates across the two diagonally opposed triangles.
If the board is even * even, then in some cases one player still has winning strategy if it manages to 'win' or 'loose' the extra column or row in a way that all the larger board rectangle can be won per previous two strategies.
If all else fails standard game tree search minmax / alphabeta / transposition tables / iterative deepening / mtd(f) or newer monte carlo search could be used. Albeit in these algos might not fit into the interview time window. This would suggest that perhaps there is a simplification that would allow optimal strategy for at least one player without having to do tree search.
I think, more clarification is needed on the question. What did the interviewer mean by "improve the AI?" Is he expecting an AI solution or an Algorithmic solution?
For Algorithmic solution, Zsombor's solution works. But to implement any of the tree-based would take more than the interview time, unless you have memorized the solution
For AI solution, we can use reinforcement learning approach. Since the environment (i.e. the board) is fully observable, we can use markov desicion process (mdp) for AI to improve. For that, we will have a define states, actions, transition probabilities, and the reward function. Then we can use Bellman's equation to estimate the optimal policy for a given board.
import java.util.Scanner;
public class TiltedDominoes {
public static void main(String[] args) throws Exception{
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int m= scn.nextInt();
int[] dp= new int[n+1];
for(int i=1;i<n;i++) {
if(i<m) {
dp[i]=1;
}else if(i==m) {
dp[i]=2;
}else {
dp[i]=dp[i-1]+dp[i-m];
}
}
System.out.println(dp[n-1]);
}
}
- ymorag June 04, 2020