Data Structures & AlgorithmsModerateTCS NQT · simulation

Problem: How many legal moves does a knight have from the corner (0,0) of an 8 x 8 board? Run the code.

Problem: How many legal moves does a knight have from the corner (0,0) of an 8 x 8 board? Run the code.
moves = [(1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), (2, -1), (-1, -2), (-2, -1)]
print(sum(1 for dx, dy in moves if 0 <= 0 + dx < 8 and 0 <= 0 + dy < 8))
What is printed?
  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 8

Correct answer: C. 2

From the corner only the offsets (1,2) and (2,1) land on the board, so the knight has 2 moves. The destination square is (0+dx, 0+dy), which must lie inside the 8 x 8 board.

More Coding Interview Problems questions

Exams that ask this topic