Response Time Across a City Grid
Updated
Problem statement
A rectangular city grid contains blocked cells (0), locations needing a response (1), and fire stations (2). Every station begins responding at time zero. A response can move one cell up, down, left, or right, taking one minute per step. It cannot enter a blocked cell. Stations operate simultaneously.
Return the earliest time at which every location marked 1 can be reached from some station. Return -1 if any such location cannot be reached. Return 0 when no location needs a response.
Station cells are passable.
Function
cityResponseTime(grid: int[][]) → intExamples
Example 1
grid = [[2,1,1],[1,0,1],[1,1,2]]return = 2Both stations start together. Every marked location is within two steps of a station.
Example 2
grid = [[2,0,1]]return = -1The blocked cell separates the only location from the station.
Example 3
grid = [[0,2],[2,0]]return = 0No cell is marked as needing a response.
Constraints
1 <= grid.length, grid[0].length <= 100.- All rows have the same length and contain only
0,1or2. - A location is reached when a response first arrives; independent stations do not block each other.
More LinkedIn Onsite Interview questions
- Allocate Mailboxes with Squared DistanceONSITE INTERVIEW · Seen Nov 2026
- Find Leaves of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Max Points on a LineONSITE INTERVIEW · Seen Nov 2026
- Explore an Unknown Grid and Find a PathONSITE INTERVIEW · Seen Nov 2026
- Fire Station Response BillONSITE INTERVIEW · Seen Nov 2026
- Roll a Weighted DieONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Subarray With at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- Kth Closest Element in a BSTONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Window with at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- LRU Cache OperationsONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
- Minimum Distance to Return a BookGoogle · ONSITE INTERVIEWGoogle interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Shortest Grid Path with Obstacle EliminationsAmazon · ONSITE INTERVIEW
- Shortest Path in a Grid with Obstacles EliminationBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Shortest Path in a Blocked GridSpotify · PHONE SCREEN
- Minimum MovesHarness · OA
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
- Minimum Knight MovesMcKinsey & Company · OAMcKinsey & Company interview questions