Quick 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.

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

  1. Every cell costs at least 1 to enter, so a route that uses the least charge never enters the same cell twice.
  2. The battery only limits which stations count: a station is reachable exactly when its cheapest route fits within battery.
  3. 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.

Cheapest Reachable Charging Station Route on a Weighted Grid

hardRivianSoftware Engineer

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

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

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.

Examples

Example 1
Input
([[7]], [0, 0], [[0, 0]], 1000000)
Output
[[0, 0]]
Notes
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.
Example 2
Input
([[3, 1], [2, 5]], [1, 1], [[0, 0], [0, 1], [1, 0], [1, 1]], 0)
Output
[[1, 1]]
Notes
Every cell is a station, start included, so battery 0 still returns [start].

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def route_to_nearest_charger(grid: list[list[int]], start: list[int], chargers: list[list[int]], battery: int) -> list[list[int]]:
Sample call
route_to_nearest_charger([[7]], [0, 0], [[0, 0]], 1000000)
Language
Case 1
Input
([[7]], [0, 0], [[0, 0]], 1000000)
Expected
[[0, 0]]
Case 2
Input
([[3, 1], [2, 5]], [1, 1], [[0, 0], [0, 1], [1, 0], [1, 1]], 0)
Expected
[[1, 1]]

Browse More Questions

Show the approach

Approach

Treat the cost of reaching a cell as the pair (total charge, steps), compared by charge first and by steps second. Entering cell (r, c) adds (grid[r][c], 1) to the pair of the cell the car came from. Every weight is at least 1, so the pair strictly grows on every move, and a priority-queue shortest-path search from start settles each cell at its least pair. The references pack the pair into one integer, charge * 4096 + steps. A least-charge route never revisits a cell, so it has at most 2499 steps (one fewer than the 2500 cells of the largest grid), and the packing keeps the order.

A station is reachable exactly when the charge part of its least pair is at most battery. Among reachable stations take the smallest pair; if there is none, return []. Call a move from u to a neighbor v tight when pair(v) = pair(u) + (grid[v], 1). Every route that achieves the smallest pair uses only tight moves, and every chain of tight moves from start to such a station is a best route. Walking tight moves backward from all stations that share the smallest pair marks exactly the cells that lie on some best route.

The route is then built from the front. From the current cell, try the neighbors in [row, column] order, which is up, left, right, down, and step to the first one that is joined by a tight move and is marked. All best routes have the same number of cells, so taking the smallest possible cell at each position gives the lexicographically smallest best route, including across different stations. The walk stops on a station with the smallest pair. It cannot pass through another station first, because that station would be strictly cheaper and would itself be the answer.

Edge cases: start on a station returns [start] before any search, whatever the battery. Battery 0 with start off a station returns [] because every move costs at least 1. When stations tie on charge and steps, comparing whole routes from the front can pick a station with larger coordinates.

Time complexity:
O(m * n * log(m * n))
Space complexity:
O(m * n)