Magicsheet logo

Find Minimum Time to Reach Last Room I

Medium
12.5%
Updated 8/1/2025

Find Minimum Time to Reach Last Room I

What is this problem about?

The Find Minimum Time to Reach Last Room I interview question is a pathfinding problem on a 2D grid. Each cell (r,c)(r, c) has a "move-in" time. You start at (0,0)(0, 0) at time t=0t=0. You can move to an adjacent cell only if the current time is greater than or equal to that cell's required time. If you arrive early, you must wait until the cell "opens." Each move itself takes 1 second. You need to find the minimum time to reach the bottom-right corner.

Why is this asked in interviews?

Companies like Uber and Google use the Find Minimum Time to Reach Last Room coding problem to assess your knowledge of Shortest Path algorithms on a graph. It’s a twist on standard grid traversal because the "weight" of an edge is dynamic—it depends on your arrival time. It tests your ability to adapt Dijkstra's algorithm to handle waiting times.

Algorithmic pattern used

This problem is solved using Dijkstra's Algorithm with a Priority Queue.

  1. Priority Queue: Store tuples (time, row, col), always exploring the state with the minimum time first.
  2. Waiting Logic: When moving from (r1,c1)(r1, c1) to (r2,c2)(r2, c2) at currentTime:
    • The time you can enter (r2,c2)(r2, c2) is max(currentTime, moveInTime[r2][c2]).
    • After entering, you spend 1 second. New time: max(currentTime, moveInTime[r2][c2]) + 1.
  3. Visited Matrix: Maintain a minTime[row][col] matrix to avoid redundant explorations.

Example explanation

Grid: [[0, 3], [3, 3]]

  1. Start at (0,0) at t=0t=0.
  2. Neighbors: (0,1) and (1,0). Both require t=3t=3.
  3. Move to (0,1): t=max(0,3)+1=4t = max(0, 3) + 1 = 4.
  4. Move from (0,1) to (1,1): t=max(4,3)+1=5t = max(4, 3) + 1 = 5. Result: 5.

Common mistakes candidates make

  • Using BFS: Basic BFS assumes all edges have weight 1. Here, the "weight" is 1+wait_time1 + wait\_time, so Dijkstra is required.
  • Off-by-one: Forgetting to add the 1 second required for the actual movement after the waiting period.
  • Infinite Loop: Not using a visited array or a minTime array to prune paths that are already known to be sub-optimal.

Interview preparation tip

Master Dijkstra on grids. Practice problems where the "cost" of a move is not just a constant but a function of your current state. This is a common Matrix interview pattern for advanced roles.

Similar Questions