Distance From Every City Block to the Nearest Charging Station
Last updated: November 29, 2026
Quick Overview
On a grid city map where 'C' marks an EV charging station, '#' a blocked block, and '.' an open block, return a grid giving each open block's walking distance (4-directional steps) to the nearest charger, or -1 if none is reachable.
Uber
November 29, 20260
0
0 solved
On a grid city map where 'C' marks an EV charging station, '#' a blocked block, and '.' an open block, return a grid giving each open block's walking distance (4-directional steps) to the nearest charger, or -1 if none is reachable.
Uber's push to electrify its fleet makes charger proximity a real feature for driver guidance. In the screen the interviewer looks for multi-source BFS, starting from every charger at once, instead of running a separate BFS from each block.
What the Interviewer Expects
- Recognize multi-source BFS from all chargers simultaneously
- Explain why one BFS gives the nearest charger for every cell
- Mark visited at enqueue time to avoid duplicates
- Leave blocked cells and unreachable cells as -1
- State O(R x C) time and space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if moving through some blocks costs 2 steps (hills)? Which algorithm then?
- Return which charger is nearest for each block, not only the distance
- How would this change on a hexagonal grid such as H3 cells?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list of strings, each char in
{'C', '#', '.'}. - Output: integer grid; chargers get 0, blocked cells and unreachable open cells get -1.
- Moves: up, down, left, right....
High-Level Design
Running BFS from every open cell is O((RC)^2). Reverse the direction: push all chargers into the queue at distance 0 and run one BFS. BFS explores in nondecreasing distance order, so the first time a ...