Interview gone wrong
I have a very basic question which I usually ask in a interview which is to implement a tic tac toe game. I like this because the logic is straightforward and it helps to judge things like code quality / speed / conciseness etc.
The candidate this time was programming in python and had put a statement to figure out if we have a winner by checking if all the elements in the diagonal position are the same.
if (cell[0][0] == cell[1][1] == cell[2][2]) {
return Winner
}
cell[x][y]
can contain a char where the value would be either '-'
or 'X'
or 'Y'
I didn't want to nit pick but it felt that the statement even though logical, is not technically right, because if we start from the left cell[0][0] == cell[1][1]
would evaluate to True
and then True == cell[2][2]
would evaluate to False
given that the cell contains a char.
And this is how it would definitely happen if the code was written in javascript
I told the candidate about the same and he agreed but he was confused at the same time stating that he has used / seen this expression multiple times in the existing codebase.
After the interview I found out Python has a thing called chained expressions when you write something like the above, it gets transformed to the later statement given below :
if (cell[0][0] == cell[1][1] == cell[2][2]) {
return Winner
}
if ((cell[0][0] == cell[1][1]) and (cell[1][1] == cell[2][2])) {
return Winner
}
Well the code is indeed correct 🤷. Well done, Python… for finding yet another way to confuse us all.