Magicsheet logo

Available Captures for Rook

Easy
97.8%
Updated 6/1/2025

Asked by 3 Companies

Available Captures for Rook

What is this problem about?

The Available Captures for Rook interview question is a grid-based simulation problem. You are given an 8x8 chessboard with one white rook ('R'), several white bishops ('B'), some black pawns ('p'), and empty squares ('.'). A rook moves horizontally or vertically. It can capture a pawn if it reaches it without hitting a bishop or the edge of the board first. Your task is to count how many pawns the rook can capture. This Available Captures for Rook coding problem is about directional traversal in a matrix.

Why is this asked in interviews?

Companies like Square and Google use this to test basic matrix navigation skills. It evaluates if a candidate can correctly implement "straight-line" movement in four directions while handling blocking conditions. It's an excellent way to check for clean code and effective use of loop control (like break).

Algorithmic pattern used

This follows the Array, Matrix, Simulation interview pattern.

  1. Locate the rook 'R' in the 8x8 grid.
  2. From that (r, c) position, iterate in four directions: Up, Down, Left, and Right.
  3. In each direction, keep moving until you hit:
    • A pawn 'p' (increment count and break).
    • A bishop 'B' (break immediately, as the path is blocked).
    • The edge of the board.

Example explanation

Imagine the rook is at (4, 4).

  • Up: At (2, 4) there is a 'p'. Count becomes 1. Stop looking Up.
  • Down: At (6, 4) there is a 'B'. Stop looking Down (blocked).
  • Left: No pawns or bishops until the edge. Count stays 1.
  • Right: At (4, 7) there is a 'p'. Count becomes 2. Result: 2.

Common mistakes candidates make

  • Not Stopping at Bishops: Forgetting that white bishops block the rook's path just as much as the edge of the board does.
  • Incorrect Coordinate Bounds: Off-by-one errors when checking if the rook has reached the edge of the 8x8 grid.
  • Redundant Searching: Searching the whole board for pawns instead of only looking in the four lines of sight from the rook's specific position.

Interview preparation tip

For matrix problems involving directional movement, define a directions array like [[0,1], [0,-1], [1,0], [-1,0]]. This allows you to use a single loop to handle all four directions, making your code cleaner and less prone to repetition errors.

Similar Questions