Coding: design the backend of a two-player chess game and make sure there are no illegal moves.
I started writing some framework, and then the interviewer interrupted me and said not to write the framework, just to validate whether a bishop move from A to B is legal. The input is A and B. Assuming the class is Position with x and y coordinates as its fields, the input can be read through a.x, a.y, b.x, b.y.
First I wrote the check for whether the move is along a diagonal, i.e. abs(dr) == abs(dc), and excluded the case where the piece doesn't move at all.
A bishop can't jump over other pieces, so how do I handle that? Check square by square along the path from A to B; any piece in between blocks the move.
How do I generalize this to the other pieces? Pull out the path check that bishop, rook and queen share, and let each piece keep its own movement constraints.
The interviewer was slow and relaxed right from the start. He asked me what problem I wanted to do. I said a graph problem, he said then chess, and I said I've never played chess, so he threw a Wikipedia page about chess at me to read. He spent about 10 minutes at the beginning introducing himself and asking about my background, then ended 10 minutes early, so in practice he didn't give me much time at all. After an interview like this you just know you've failed, so the experience was super bad.
Discussion
Loading comments…