Level: Unknown Level
Round: Phone Screen · Type: Coding · Difficulty: 6/10 · Duration: 60 min · Interviewer: Unfriendly
Topics: Arrays, Two-dimensional arrays
Location: San Francisco, CA
Interview date: 2026-03-15
Got offer: False
I had a technical phone screen where I was given a coding question involving finding robots based on their distances to blockers on a 2D board.
The coding question I got was:
You are given a m * n board representing a position map and an array representing distances to the nearest blocker from a robot's position. The board is a 2D array where each cell can be:
'O': Represents a robot. 'E': Represents an empty space. 'X': Represents a blocker. The boundary of the board is also considered a blocker. Additionally, you are provided with a distance array of four integers, which correspond to the distances to the closest blocker in the following order: left, top, bottom, and right.
Write a function that takes the position map and the distance array as inputs and returns the indices of all robots that match the given distance criteria.
Constraints:
The board dimensions are at least 1x1. The distance array contains exactly four integers. The matrix only contains 'O', 'E' and 'X' Example 1:
Input: board = [["O","E","E","E","X"], ["E","O","X","X","X"], ["E","E","E","E","E"], ["X","E","O","E","E"], ["X","E","X","E","X"]] distance = [2,2,4,1]
Output: [[1,1]]
Explanation: Only the robot at position (1,1) has a distance of 2 to the left blocker, 2 to the top blocker, 4 to the bottom blocker, and 1 to the right blocker, matching the distance array.
Example 2:
Input: board = [["O","E","X","O","O"], ["E","O","X","O","X"], ["X","X","O","E","E"], ["E","O","E","O","E"], ["O","O","X","O","O"]] distance = [2,1,2,4]
Output: [[3,1]]
Example 3:
Input: board = [["O","X","O"], ["E","O","X"], ["O","X","O"]] distance = [1,1,1,1]
Output: [[2,2],[0,2]]
My proposed solution is:
`python from typing import List, Optional
class Solution: LEFT = 0 TOP = 1 BOTTOM = 2 RIGHT = 3
def findRobotsPosition(self, board: List[List[str]], distance: List[int]) -> List[List[int]]:
if not board or not board[0] or len(distance) != 4:
return []
rows, cols = len(board), len(board[0])
result = []
# Map to store distances for each robot at position (r, c)
map_ = {}
# Array to keep track of the last 'X' seen from the top for each column
top = [-1] * cols
# First pass to calculate left and top distances
for r in range(rows):
left = -1
for c in range(cols):
if board[r][c] == 'O':
distanceLeft = abs(c - left)
distanceTop = abs(r - top[c])
map_[f"{r},{c}"] = [distanceLeft, distanceTop, -1, -1]
if board[r][c] == 'X':
left = c # Update the last seen 'X' on the left
top[c] = r # Update the last seen 'X' on the top for this column
# Array to keep track of the last 'X' seen from the bottom for each column
bottom = [rows] * cols
# Second pass to calculate bottom and right distances
for r in range(rows - 1, -1, -1):
right = cols
for c in range(cols - 1, -1, -1):
if board[r][c] == 'O':
distanceBottom = abs(r - bottom[c])
distanceRight = abs(c - right)
key = f"{r},{c}"
if key in map_:
distances = map_[key]
distances[self.BOTTOM] = distanceBottom
distances[self.RIGHT] = distanceRight
# Compare with the distance parameter
if (distances[self.LEFT] == distance[self.LEFT] and
distances[self.TOP] == distance[self.TOP] and
distances[self.BOTTOM] == distance[self.BOTTOM] and
distances[self.RIGHT] == distance[self.RIGHT]):
result.append([r, c])
if board[r][c] == 'X':
right = c # Update the last seen 'X' on the right
bottom[c] = r # Update the last seen 'X' on the bottom for this column
return result
`