Databricks Interview Coding Question: Fastest Transportation Mode with Minimum Cost

14 Views
No Comments

You live in San Francisco city and want to minimize your commute time to the Databricks HQ.

Given a 2D matrix of the San Francisco grid and the time as well as cost matrix of all the available transportation modes, return the fastest mode of transportation. If there are multiple such modes, then return the one with the least cost.

Sample Input:

2D Grid:
|3|3|S|2|X|X|
|3|1|1|2|X|2|
|3|1|1|2|2|2|
|3|1|1|1|D|3|
|3|3|3|3|3|4|
|4|4|4|4|4|4|

Legend:
X = Roadblock
S = Source
D = Destination
1 = Walk, 2 = Bike, 3 = Car, 4 = Train

Transportation Modes: ["Walk", "Bike", "Car", "Train"]
Cost Matrix (Dollars/Block): [0, 1, 3, 2]
Time Matrix (Minutes/Block): [3, 2, 1, 1]

Note: In this example, we are only counting the blocks between 'S' and 'D' to compute the minimum time and dollar cost.

Sample Output: Bike

This problem asks you to compare transportation modes by total travel time across the grid path from S to D, while using total cost as a tie-breaker. A typical solution is to traverse the 2D grid, ignore roadblocks, count the effective blocks between source and destination, and then compute each mode’s total time and total cost from the per-block matrices. Finally, choose the mode with the smallest time, and if multiple modes tie, choose the one with the smallest cost. In the sample, Bike is selected.

END
 0