KatInterview

Nearest Depot and Peak-Load Routing on a Grid

Trees & GraphsModel answer
MediumModel answerTrees & GraphsBFSDijkstramulti-source

Last updated

Given a grid with depot cells, find shortest paths from query locations to any depot, then extend to pick the depot maximizing a distance-penalized load score.

Nearest Depot and Peak-Load Routing

You are working with a warehouse routing system modeled as an m × n grid of characters:

  • 'W' — a warehouse depot
  • '#' — an impassable wall
  • ' ' (space) — an open passable cell

Movement is strictly four-directional (up, down, left, right). You cannot move outside the grid or into a '#' cell. Depots ('W') are passable — a path may continue through one.

You are given a list of K query locations, each as a (row, col) pair.


Part A

Implement nearestDepotDistances(grid, queries).

For each query location, return the minimum number of steps to reach any depot. Follow these rules:

  • If the query cell itself is a depot, the distance is 0.
  • If the query cell is a wall ('#') or there is no path to any depot, return -1.

Part B (follow-up)

Each depot at coordinates (r, c) has a non-negative integer base load L[r][c]. You are also given a non-negative integer beta.

Define the effective load of a depot at grid distance d from a query location as:

effective_load = max(0, L[r][c] - beta * d)

Implement peakLoadAssignments(grid, L, beta, queries).

For each query location, among all reachable depots, return the one that maximizes effective load. Break ties in this order:

  1. Smaller grid distance d
  2. Smaller row index
  3. Smaller column index

Return both the chosen depot's coordinates (row, col) and its effective load value for that query. If no depot is reachable (or the query cell is a wall or out of bounds), return ((-1, -1), -1).


Constraints

  • 1 ≤ m, n ≤ 500
  • 1 ≤ K ≤ 10^5
  • 0 ≤ L[r][c] ≤ 10^6 for all depot cells
  • 0 ≤ beta ≤ 10^6
  • The grid contains at least one 'W' cell (though it may be unreachable from some queries)

What is expected

  • A correct, well-structured implementation of both parts
  • Clear explanation of your algorithmic approach and why you chose it
  • Time and space complexity analysis
  • Discussion of edge cases and how your code handles them

Hints

Hint 1

For Part A, think about what happens if you flip the search direction: instead of running BFS from every query toward the depots, start BFS simultaneously from all depots outward. What does the first-arrival distance at any cell represent?

Hint 2

For Part B, you need each cell to track not just a distance but which depot 'wins' it. Try encoding the tie-break priority into a single comparable key that you attach to each depot source, then think about which shortest-path algorithm preserves that key correctly as it propagates across edges.

Hint 3

Watch out for the clamping rule max(0, ...). When beta is large, many depots end up with effective load 0. In that scenario, which depot should win? Work through a small example where every depot's score clamps to zero and make sure your code produces the right answer rather than returning -1.