Pergunta de entrevista da empresa Lookout

Design an algorithm where you can detect a winning Tic-Tac-Toe game. Note: I heard anecdotally that their approach might instead focus on some card game (e.g. Poker or Blackjack or whatever), so be ready for shenanigans like this.

Resposta da entrevista

Sigiloso

22 de mai. de 2013

My first attempt was a brute force attempt. I didn't even try to come up with the (time) complexity of this, at least while I was on-site. // in Objective C everything is 0 based... but on my white board // my tic-tac-toe grid object was 1 based... // we might want to check if the game is valid even before // we waste time looking to see if there's a winning set of three - (BOOL) isValidGame: (Grid *) grid { int totX = 0; int totO = 0; for( int i = 1; i 1 ) return NO; return YES; } // this is the *brute force* solution - (NSString *) whoIsAWinner { int winnerX, winnerO; // initialize "winner" to zero... there can be at most only one winner winnerX = winnerO = 0; // first check for three X's or three Y's in a column for( int column = 1; column 1) || (winnerO > 1) { return NULL; } // tie - no winners if((winnerX == 0) && (winnerO == 0)) { return NULL; } if(winnerX == 0) && (winnerO == 1) { return @"O"; } if(winnerX == 1) && (winnerO == 0) { return @"X"; } return NULL; } Another approach I spoke about (and would have worked up had I had time within the one hour window) would have been to have separate all possible lines of three into different groups. E.G. [1,1][1,2][1,3]; [2,1][2,2][2,3];…;…;[1,1],[2,2],[3,3];[3,1][2,2][1,3] And then look for three matching symbols within each set of triplets.

2