Lowest-Energy Route to the Nearest Charging Station on a Weighted Grid
Company: Rivian
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
An electric car sits on a rectangular grid of cells. Driving into a cell uses a number of units of battery charge equal to that cell's weight, and some cells hold charging stations. Given the grid, the car's starting cell, the charging station cells and the car's current charge, return the route to the nearest charging station that the car can reach without running out of charge. "Nearest" means the route that uses the least charge in total.
### Function Signature
```python
def route_to_nearest_charger(grid: list[list[int]], start: list[int], chargers: list[list[int]], battery: int) -> list[list[int]]:
```
### Rules
- Each step moves the car one cell up, down, left or right. The car never leaves the grid, and every cell may be entered.
- Moving into cell `(r, c)` uses `grid[r][c]` units of charge. The starting cell uses nothing.
- The car starts with `battery` units and its charge may never drop below 0, so a route is feasible only when the total charge it uses is at most `battery`. Arriving with exactly 0 units left is allowed.
- Among all feasible routes that end on a charging station, return the one that uses the least total charge. If several tie, return the one with the fewest steps. If several still tie, return the one whose list of cells is lexicographically smallest, comparing cells as `[row, column]` pairs from the front of the list.
- The route is a list of `[row, column]` cells that starts with `start`, ends with the chosen charging station, and lists every visited cell in order.
- If `start` holds a charging station, return `[start]`.
- If no charging station can be reached with the available charge, return `[]`.
### Constraints
- `1 <= m, n <= 50`, where `m = len(grid)` and `n = len(grid[0])`; every row has length `n`
- `1 <= grid[r][c] <= 100`
- `start = [r, c]` with `0 <= r < m` and `0 <= c < n`
- `1 <= len(chargers) <= m * n`; every charger is an in-bounds `[r, c]`, and no charger appears twice
- `0 <= battery <= 10^6`
- Every charge total that matters is at most `10^6`, well within the 32-bit signed integer range.
### Examples
#### Example 1
```text
Input: grid = [[1,3,1],[1,5,1],[4,2,1]], start = [0,0], chargers = [[2,2],[2,0]], battery = 6
Output: [[0,0],[1,0],[2,0]]
```
Moving down twice reaches `[2,0]` using 1 + 4 = 5 units. The cheapest route to `[2,2]` (right, right, down, down) uses 3 + 1 + 1 + 1 = 6 units. The station at `[2,0]` is nearer, and the car arrives with 1 unit left.
#### Example 2
```text
Input: grid = [[1,3,1],[1,5,1],[4,2,1]], start = [0,0], chargers = [[2,2]], battery = 5
Output: []
```
The cheapest route to the only station uses 6 units, more than the 5 available.
#### Example 3
```text
Input: grid = [[1,1],[1,1]], start = [0,0], chargers = [[1,1]], battery = 2
Output: [[0,0],[0,1],[1,1]]
```
Both routes to `[1,1]` use 2 units and 2 steps. Their second cells are `[0,1]` and `[1,0]`, and `[0,1]` is lexicographically smaller.
Overview: Find the route an electric car should take across a weighted grid to reach the nearest charging station without running out of battery, where entering each cell uses charge. The problem tests path search under a resource limit, pruning routes that cannot be feasible and a deterministic tie-break between equal routes.
Read the full Rivian Software Engineer interview experience this question came from
An electric car sits on a rectangular grid of cells. Driving into a cell uses a number of units of battery charge equal to that cell's weight, and some cells hold charging stations. Given the grid, the car's starting cell, the charging station cells and the car's current charge, return the route to the nearest charging station that the car can reach without running out of charge. "Nearest" means the route that uses the least charge in total.
Implement `route_to_nearest_charger(grid, start, chargers, battery)`.
### Rules
- Each step moves the car one cell up, down, left or right. The car never leaves the grid, and every cell may be entered.
- Moving into cell `(r, c)` uses `grid[r][c]` units of charge. The starting cell uses nothing.
- The car starts with `battery` units and its charge may never drop below 0, so a route is feasible only when the total charge it uses is at most `battery`. Arriving with exactly 0 units left is allowed.
- Among all feasible routes that end on a charging station, return the one that uses the least total charge. If several tie, return the one with the fewest steps. If several still tie, return the one whose list of cells is lexicographically smallest, comparing cells as `[row, column]` pairs from the front of the list.
- The route is a list of `[row, column]` cells that starts with `start`, ends with the chosen charging station, and lists every visited cell in order.
- If `start` holds a charging station, return `[start]`.
- If no charging station can be reached with the available charge, return `[]`.
### Input and output
- `grid`: `m` rows of `n` integer weights (`int[][]` in Java, `const std::vector<std::vector<int>>&` in C++).
- `start`: the starting cell as `[row, column]` (`int[]` in Java, `const std::vector<int>&` in C++).
- `chargers`: one list holding every charging station cell as `[row, column]` (`int[][]` in Java, `const std::vector<std::vector<int>>&` in C++).
- `battery`: the starting charge (`int`).
- Return the route as a list of `[row, column]` pairs (`int[][]` in Java, `std::vector<std::vector<int>>` in C++), or an empty list when no station can be reached.
All values, including every charge total that matters, fit in a 32-bit signed integer (none exceeds 2^31-1), so Java and C++ use `int`.
### Constraints
- `1 <= m, n <= 50`, where `m = len(grid)` and `n = len(grid[0])`; every row has length `n`
- `1 <= grid[r][c] <= 100`
- `start = [r, c]` with `0 <= r < m` and `0 <= c < n`
- `1 <= len(chargers) <= m * n`; every charger is an in-bounds `[r, c]`, and no charger appears twice
- `0 <= battery <= 10^6`
- Every charge total that matters is at most `10^6`, well within the 32-bit signed integer range.
### Example 1
```text
Input: grid = [[1,3,1],[1,5,1],[4,2,1]], start = [0,0], chargers = [[2,2],[2,0]], battery = 6
Output: [[0,0],[1,0],[2,0]]
```
Moving down twice reaches `[2,0]` using 1 + 4 = 5 units. The cheapest route to `[2,2]` (right, right, down, down) uses 3 + 1 + 1 + 1 = 6 units. The station at `[2,0]` is nearer, and the car arrives with 1 unit left.
### Example 2
```text
Input: grid = [[1,1],[1,1]], start = [0,0], chargers = [[1,1]], battery = 2
Output: [[0,0],[0,1],[1,1]]
```
Both routes to `[1,1]` use 2 units and 2 steps. Their second cells are `[0,1]` and `[1,0]`, and `[0,1]` is lexicographically smaller.
Constraints
- 1 <= m, n <= 50, where m = len(grid) and n = len(grid[0]); every row has length n
- 1 <= grid[r][c] <= 100
- start = [r, c] with 0 <= r < m and 0 <= c < n
- 1 <= len(chargers) <= m * n; every charger is an in-bounds [r, c], and no charger appears twice
- 0 <= battery <= 10^6
- Every charge total that matters is at most 10^6, well within the 32-bit signed integer range.
Examples
Input: ([[7]], [0, 0], [[0, 0]], 1000000)
Expected Output: [[0, 0]]
Explanation: 1x1 grid whose only cell is both the start and the only station, so the route is [start]; battery is at its bound 10^6.
Input: ([[3, 1], [2, 5]], [1, 1], [[0, 0], [0, 1], [1, 0], [1, 1]], 0)
Expected Output: [[1, 1]]
Explanation: Every cell is a station, start included, so battery 0 still returns [start].
Hints
- Every cell costs at least 1 to enter, so a route that uses the least charge never enters the same cell twice.
- The battery only limits which stations count: a station is reachable exactly when its cheapest route fits within battery.
- When best routes tie on charge and steps, they are compared at the first position where their cells differ, counted from start, even if they end at different stations.