Find the Nearest Charging Station for Every Vehicle

Quick Overview

Practice multi-source BFS to compute the nearest charging station for every vehicle in a grid. Work through empty inputs, missing stations, distance ties, and the distinction between nearest distances and exclusive assignments.

Find the Nearest Charging Station for Every Vehicle

Company: Spotify

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Given a rectangular grid whose cells are `C` (charging station), `V` (vehicle), or `.` (empty), return the shortest distance from every vehicle to a charging station. Movement is one cell up, down, left, or right. The base problem has no obstacles; vehicles and stations do not block movement. ### Input and Output For this practice interface, return records `[row, column, distance]` for vehicle cells in row-major order, using zero-based coordinates. Use distance `-1` when there is no station. An empty grid or a grid with no vehicles returns an empty list. These output conventions normalize an interview prompt that did not specify a function signature; numerical grid-size bounds were not reported. For example, `[["V",".","C"],[".","V","."]]` returns `[[0,0,2],[1,1,2]]`. ### Clarifying Questions Why can the base-case distance also be described as Manhattan distance? What would change if some cells were walls? Can you avoid a separate visited set while retaining the original vehicle locations? ### What a Strong Answer Covers Explain the repeated-search baseline, then give a multi-source breadth-first search, its first-discovery invariant, and time and space in terms of grid rows and columns. Cover empty input, absent stations, adjacent vehicles and stations, and several equally near stations. ### Follow-up Questions How would you add obstacles? Why is finding each vehicle's nearest station different from assigning distinct stations to all vehicles?

Overview: Practice multi-source BFS to compute the nearest charging station for every vehicle in a grid. Work through empty inputs, missing stations, distance ties, and the distinction between nearest distances and exclusive assignments.

Spotify logo
Spotify
Oct 18, 2026
hardSoftware EngineerOnsiteCoding & Algorithms
0
0

Given a rectangular grid whose cells are C (charging station), V (vehicle), or . (empty), return the shortest distance from every vehicle to a charging station. Movement is one cell up, down, left, or right. The base problem has no obstacles; vehicles and stations do not block movement.

Input and Output

For this practice interface, return records [row, column, distance] for vehicle cells in row-major order, using zero-based coordinates. Use distance -1 when there is no station. An empty grid or a grid with no vehicles returns an empty list. These output conventions normalize an interview prompt that did not specify a function signature; numerical grid-size bounds were not reported.

For example, [["V",".","C"],[".","V","."]] returns [[0,0,2],[1,1,2]].

Clarifying Questions Guidance

Why can the base-case distance also be described as Manhattan distance? What would change if some cells were walls? Can you avoid a separate visited set while retaining the original vehicle locations?

What a Strong Answer Covers Guidance

Explain the repeated-search baseline, then give a multi-source breadth-first search, its first-discovery invariant, and time and space in terms of grid rows and columns. Cover empty input, absent stations, adjacent vehicles and stations, and several equally near stations.

Follow-up Questions Guidance

How would you add obstacles? Why is finding each vehicle's nearest station different from assigning distinct stations to all vehicles?