KatInterview

Grid Walk With Clockwise Redirects at Walls and Obstacles

Arrays & HashingAuto-graded · 27 test cases
MediumAuto-gradedArrays & Hashingmatrixsimulationgrid

Last updated

Tests grid simulation by traversing a matrix in a right-biased clockwise walk that turns at boundaries, visited cells, and blocked positions until no move is possible.

You have an m x n grid of integers. Your task is to collect its values in the order a clockwise spiral walk visits them, extended to handle obstacle cells that redirect the walker.

Walk rules

The walker begins at the top-left corner (0, 0), facing right. That starting cell is immediately collected.

At every subsequent step the walker inspects the cell directly ahead. If that cell is reachable, the walker moves into it and collects its value. If not, the walker rotates 90 degrees clockwise and inspects the next candidate, repeating until a reachable cell is found or all four directions have been checked. The walker commits to the first direction that leads to a reachable cell.

A cell is reachable if all three of the following hold:

  1. It lies within the grid bounds.
  2. It has not been visited yet.
  3. It is not an obstacle.

The walk ends the moment all four directions fail. Any unvisited, non-obstacle cells the walk never reaches are simply omitted from the output.

Obstacles are never collected and never visited; they exist only to redirect or stop the walker.

Direction conventions

  • Right: (r, c) → (r, c + 1)
  • Down: (r, c) → (r + 1, c)
  • Left: (r, c) → (r, c − 1)
  • Up: (r, c) → (r − 1, c)

Clockwise rotation order: right → down → left → up → right → …

Obstacles

Obstacles are supplied as a list of [row, col] pairs. A grid with an empty obstacle list produces the plain clockwise spiral order; both cases are handled by the same function.

Constraints

  • 1 <= m, n <= 100, where m = len(grid) and n = len(grid[0]); every row has exactly n cells.
  • -10^9 <= grid[r][c] <= 10^9
  • 0 <= len(obstacles) <= m * n - 1
  • Every pair in obstacles is inside the grid, all pairs are distinct, and [0, 0] is never an obstacle.

Examples

Example 1

Input: spiral_with_blockers([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [])

Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]

Example 2

Input: spiral_with_blockers([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [[1, 1]])

Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5]

Example 3

Input: spiral_with_blockers([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 1]])

Output: [1, 4, 7, 8, 9, 6, 3]

Hints

Hint 1

Instead of computing spiral layers with shrinking boundaries, simulate the walk step by step with three pieces of state: current position, current direction, and which cells are still available to enter.

Hint 2

Obstacles and already-visited cells block movement in exactly the same way. Store both in one boolean grid so you only need one reachability check — no special cases for obstacles versus visited.

Hint 3

At each step, try the current direction first, then rotate clockwise up to three more times. Moving in the first direction that works and stopping the entire walk when all four fail directly encodes the problem rules.