Ctrl K

Queens Eyeing the King

Medium
On an 8 × 8 chessboard (cells numbered from 0) there are several black queens and one white king. queens[i] = [x, y] is the cell of the i-th queen and king = [x, y] is the king's cell. A queen attacks the king if they share a row, a column or a diagonal and no other piece stands between them. Return the coordinates of every queen that attacks the king, in any order.
Example 1
queens=[[2, 2], [5, 5], [2, 6], [6, 2], [4, 7]], king=[4, 4] → [[2, 2], [2, 6], [4, 7], [6, 2], [5, 5]]

The queens [2, 2], [5, 5], [2, 6] and [6, 2] see the king along diagonals. The queen [4, 7] shares the king's row with nothing in between, so it attacks too.

Example 2
queens=[[0, 1], [0, 3], [3, 0]], king=[0, 0] → [[0, 1], [3, 0]]

The queen [0, 3] is blocked by [0, 1], while [3, 0] attacks along the column.

Example 3
queens=[[1, 2], [2, 1]], king=[0, 0] → []

Both queens are a knight's move away from the king, so no line connects them.

Constraints
  • 1 ≤ len(queens) < 64
  • 0 ≤ x, y < 8 for every queen and the king
  • All pieces stand on different cells
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep MediumMatch-Three Cascade Medium
loading Python…
Input
queens=[[2, 2], [5, 5], [2, 6], [6, 2], [4, 7]], king=[4, 4]
Expected
[[2, 2], [2, 6], [4, 7], [6, 2], [5, 5]]

Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.