Find Fastest Commute Mode
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates graph traversal and shortest-path reasoning on a constrained 2D grid, including modeling movement rules per transportation mode, reachability analysis, and multi-criteria optimization (minimizing total travel time with cost as a tie-breaker).
Constraints
- 1 <= number of rows, number of columns <= 200
- grid contains only 'S', 'D', 'X', '1', '2', '3', '4'
- There is exactly one 'S' and exactly one 'D'
- time and cost each contain exactly 4 positive integers
- At least one transportation mode can reach the destination
Examples
Input: (['1111111', 'SXXXXXD', '2222222'], [2, 1, 3, 4], [1, 2, 1, 1])
Expected Output: 'Bike'
Explanation: Walk and Bike are both reachable in 8 blocks. Walk takes 8 * 2 = 16 minutes, while Bike takes 8 * 1 = 8 minutes. Car and Train are unreachable.
Input: (['1111111', 'SXXXXXD', '2222222'], [1, 1, 5, 6], [1, 2, 1, 1])
Expected Output: 'Walk'
Explanation: Walk and Bike both need 8 blocks and both take 8 minutes. Walk costs 8 * 1 = 8, while Bike costs 8 * 2 = 16, so Walk wins the tie.
Input: (['S222D'], [5, 1, 3, 4], [7, 2, 5, 6])
Expected Output: 'Bike'
Explanation: Only Bike can use the cells between S and D. The shortest path is 4 blocks, so Bike is the only valid answer.
Input: (['S44', 'XX4', 'XXD'], [1, 1, 1, 2], [1, 1, 1, 3])
Expected Output: 'Train'
Explanation: Only Train can reach D by following the 4-cells: S -> 4 -> 4 -> 4 -> D, which is 4 blocks.
Input: (['SD'], [3, 2, 2, 5], [9, 4, 1, 1])
Expected Output: 'Car'
Explanation: S and D are adjacent, so every mode can reach D in 1 block. Bike and Car both take 2 minutes, but Car costs 1 while Bike costs 4.
Hints
- For a fixed mode, every move has equal weight, so the best route is the one with the fewest blocks.
- Try running a BFS from 'S' to 'D' once for each of the 4 modes, allowing only 'S', 'D', and that mode's cells.