ENGINEERING · · 1 MIN READ

Interview gone wrong

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, and conciseness. One candidate's Python answer taught me something new.

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:

python
if (cell[0][0] == cell[1][1] == cell[2][2]):
    return Winner

At 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:

python
if ((cell[0][0] == cell[1][1]) and (cell[1][1] == cell[2][2])):
    return Winner

The code is indeed correct. Python has yet another way to confuse us all.

Ashutosh Singh

Technical co-founder of Bik.ai, where we help ecommerce brands automate customer support and marketing. I write about running the startup, developer productivity, engineering, and AI. Based between New York and Bengaluru.