KatInterview

Grid Sentinels: Match Robots by Barrier Distance Profile

Arrays & HashingAuto-graded · 20 test cases
MediumAuto-gradedArrays & Hashingmatrixprefix-scansimulation

Last updated

Tests efficient 2-D prefix sweeps to compute each robot's nearest-wall distance in four directions and filter by an exact profile query.

Grid Sentinels: Match Robots by Barrier Distance Profile

You are given a rectangular grid and a four-element profile. Each cell in the grid is one of three things: a robot ('R'), a barrier ('#'), or an open cell ('.'). For every robot, you can measure how far it sits from the nearest barrier in each cardinal direction — treating the boundary of the grid itself as a barrier.

Your task: return the [row, col] position of every robot whose four-directional barrier distances match the profile exactly.

Distance rules

Label the grid rows 0 to m-1 (top to bottom) and columns 0 to n-1 (left to right).

For a robot at position (r, c), define:

DirectionValue
dist_upr - i, where i is the largest row index satisfying i < r and grid[i][c] == '#'; use i = -1 if no such row exists
dist_downi - r, where i is the smallest row index satisfying i > r and grid[i][c] == '#'; use i = m if no such row exists
dist_leftc - j, where j is the largest column index satisfying j < c and grid[r][j] == '#'; use j = -1 if no such column exists
dist_rightj - c, where j is the smallest column index satisfying j > c and grid[r][j] == '#'; use j = n if no such column exists

A few things to note:

  • Only '#' cells act as barriers. Other robots and open cells are transparent — the sweep continues through them as if they were not there.
  • A robot sitting immediately beside a barrier (or beside the grid edge) has distance 1 in that direction, so every distance is at least 1.

Profile matching

The query profile is [up, down, left, right]. A robot matches when all four of its distances equal the corresponding profile values simultaneously.

Return every matching robot's position as [row, col], sorted first by row and then by column within the same row. Return an empty list when no robot matches.

Constraints

  • 1 <= m <= 500 and 1 <= n <= 500, where m = len(grid) and n = len(grid[0])
  • All rows have the same length
  • Every character in grid is 'R', '#', or '.'
  • len(profile) == 4
  • 1 <= profile[k] <= 10^9 for every k

Examples

Example 1

Input: locate_matching_robots(['R.#.R', '.R...', '#.R.R'], [2, 2, 2, 4])

Output: [[1, 1]]

Example 2

Input: locate_matching_robots(['R#R.', '....', 'R#R#'], [3, 1, 1, 1])

Output: [[2, 0], [2, 2]]

Example 3

Input: locate_matching_robots(['R'], [1, 1, 1, 2])

Output: []

Hints

Hint 1

A brute force that scans outward from each robot in all four directions costs O(m·n·(m+n)) in the worst case — think about how a single pass over the grid could give you the distance for every cell at once.

Hint 2

Process each column top-to-bottom while tracking the row of the most recent '#' seen (use -1 as a sentinel for the top edge). The same idea with reversed traversal and the sentinel m handles the downward distances. Then apply the same logic row-by-row for left and right.

Hint 3

Only '#' should update your tracker. If you also update on 'R', robots in the line of sight will incorrectly block the sweep — verify with a case where two robots share a column with no barrier between them.