The main coding problem involved finding the closest charging station for electric vehicles in a two-dimensional grid. The basic version could be solved using Multi-Source BFS, but related interview variants introduced one-dimensional arrays, unique vehicle-to-charger assignments, shortest paths with obstacles, and streaming data.
Interview Details
Coding — Find the Closest Charging Station on a Grid
Given a two-dimensional grid containing electric vehicles, charging stations, and empty cells, find the shortest distance from every vehicle to its nearest charging station. Each cell contains one of three values:
C: Charging station
V: Electric vehicle
.: Empty cell
Movement is permitted in four directions: up, down, left, and right. Diagonal movement is not allowed, and the initial version contains no obstacles. The distance is measured by the minimum number of grid steps required to reach a charging station.
Example
Input:
grid = [
["V", ".", ".", "C"],
[".", "V", ".", "."],
[".", "C", ".", "V"]
]
Output:
{
(0, 0): 3,
(1, 1): 1,
(2, 3): 2
}
Explanation:
Vehicle (0, 0) is 3 steps from its closest charger.
Vehicle (1, 1) is 1 step from charger (2, 1).
Vehicle (2, 3) is 2 steps from its closest charger.
Input:
grid = [
["V", ".", ".", "C"],
[".", "V", ".", "."],
[".", "C", ".", "V"]
]
Output:
{
(0, 0): 3,
(1, 1): 1,
(2, 3): 2
}
Explanation:
Vehicle (0, 0) is 3 steps from its closest charger.
Vehicle (1, 1) is 1 step from charger (2, 1).
Vehicle (2, 3) is 2 steps from its closest charger.
The challenge is to compute the nearest-charger distance for every vehicle efficiently, rather than running an independent search from each vehicle.
Part 1 — Brute Force: BFS from Every Vehicle
One straightforward approach is to run BFS separately from each vehicle. For every V cell, the algorithm explores neighboring cells until it reaches a charging station. Since movement is restricted to four directions and each move has equal cost, BFS finds the shortest distance. However, this approach repeats much of the same work when multiple vehicles are present.
Let:
R be the number of grid rows.
C be the number of grid columns.
V be the number of vehicles.
The worst-case time complexity is O(V × R × C), because each vehicle may require exploring the entire grid.
Part 2 — Optimized Approach: Multi-Source BFS
The more efficient solution reverses the search direction. Instead of running BFS from every vehicle, start BFS from all charging stations simultaneously. Each charging station begins with a distance of zero, and the search expands outward to calculate the minimum distance to any charger. Because BFS processes cells in increasing order of distance, the first time a cell is reached determines its shortest distance to a charging station.
Python Implementation
The following reference implementation uses a distance matrix and initializes the queue with every charging station.
from collections import deque
defnearestChargingStations(grid):ifnot grid ornot grid[0]:return{} rows =len(grid) cols =len(grid[0]) distances =[[-1]* cols for _ inrange(rows)] queue = deque()for r inrange(rows):for c inrange(cols):if grid[r][c]=="C": distances[r][c]=0 queue.append((r, c)) directions =[(1,0),(-1,0),(0,1),(0,-1)]while queue: r, c = queue.popleft()for dr, dc in directions: nr = r + dr
nc = c + dc
ifnot(0<= nr < rows):continueifnot(0<= nc < cols):continueif distances[nr][nc]!=-1:continue distances[nr][nc]= distances[r][c]+1 queue.append((nr, nc)) result ={}for r inrange(rows):for c inrange(cols):if grid[r][c]=="V": result[(r, c)]= distances[r][c]return result
from collections import deque
defnearestChargingStations(grid):ifnot grid ornot grid[0]:return{} rows =len(grid) cols =len(grid[0]) distances =[[-1]* cols for _ inrange(rows)] queue = deque()for r inrange(rows):for c inrange(cols):if grid[r][c]=="C": distances[r][c]=0 queue.append((r, c)) directions =[(1,0),(-1,0),(0,1),(0,-1)]while queue: r, c = queue.popleft()for dr, dc in directions: nr = r + dr
nc = c + dc
ifnot(0<= nr < rows):continueifnot(0<= nc < cols):continueif distances[nr][nc]!=-1:continue distances[nr][nc]= distances[r][c]+1 queue.append((nr, nc)) result ={}for r inrange(rows):for c inrange(cols):if grid[r][c]=="V": result[(r, c)]= distances[r][c]return result
This implementation returns a mapping from vehicle coordinates to their nearest-charger distances. It uses -1 when no charging station exists, as an illustrative convention for the otherwise unspecified no-charger case.
Time and Space Complexity
Time Complexity:O(R × C) — Each grid cell is processed at most once.
Auxiliary Space Complexity:O(R × C) — The distance matrix and BFS queue can each contain up to R × C elements.
The key improvement is that all vehicles share
Unlock Full Details
Create a free account to unlock this interview experience report.