Google questions

Grid Signals

GoogleOnsiteMedium

A grid is a list of strings of equal length. S is a signal source, T is a target that must receive a signal, B is a blocker, and . is an empty cell. All sources start emitting at time 0, and each time unit a signal moves from a cell to each of its four neighbours. Signals pass through empty cells, sources and targets, and never enter a blocker. A target receives the signal at a time equal to its distance to the nearest source.

Part 1: Time to reach every target

Return [time], where time is the earliest time at which every target has received a signal. Return -1 as the time if some target is never reached, and 0 if the grid has no targets.

  1. Example 1
    grid
    ["S..T","...."]
    Output
    [3]

    Why: The signal needs 3 steps to travel from the S cell to the target across the top row, so the answer is [3].

Constraints

  • 1 <= len(grid) <= 30
  • 1 <= len(grid[0]) <= 30
  • all rows of grid have the same length
  • grid[i][j] is one of S, T, B, .
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Grid Signals: Google interview question | LoopCoach