I use tic tac toe as one of the interview questions, the logic is straightforward and it helps to judge things like code quality / speed / conciseness.
The surprising comparison#
One candidate who was a Python developer wrote something like this:
if (cell[0][0] == cell[1][1] == cell[2][2]):
return WinnerAt first I thought this was logically flawed. Since cell[x][y] contains characters ('-', 'X', or 'Y'), the expression True == cell[2][2] would return False in JavaScript.
Python's chained expressions#
However, Python handles this differently through chained expressions. Python transforms the above into:
if ((cell[0][0] == cell[1][1]) and (cell[1][1] == cell[2][2])):
return WinnerThe code is indeed correct. Python has yet another way to confuse us all.